spring 注解配置
以前刚开始学spring的时候,我们最初用的配置是通过XML的方式读取bean,而如今随着SpringBoot跟SpringCloud的流行,越来越多的企业都开始使用Spring注解方式获取Bean.
1 @Configuration 类似Spring中的xml配置文件
主要pom.xml
<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.12.RELEASE</version> </dependency> </dependencies>
2 Person类
package com.melon.bean; public class Person { private String name; private Integer age; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + '}'; } }
2 spring的bean.xml
<?xml version="1.0" encoding="UTF-8" ?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"> <!--2 xml配置的包扫描 只要标注了@controller @service @Repository @component 任何一个组件--> <!--<context:component-scan base-package="com.melon"></context:component-scan>--> <!--1第一节xml bean 配置 跟 @bean--> <bean id="person" class="com.melon.bean.Person"> <property name="age" value="10"></property> <property name="name" value="zhangsan"></property> </bean> </beans>
3 原始的测试方式
package com.melon.bean; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class SpringTest { public static void main(String[] args) { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml"); Person bean = (Person)applicationContext.getBean("person"); System.out.println(bean); } }
结果成功了。
下面改成Spring注解方式读取bean,也就是不需要bean.xml,然后Perons类不变,多个Config类
@Configuration public class MainConfig { //给容器注册个Bean,类型为返回的类型,id默认是用方法名作为id @Bean public Person person(){ Person person = new Person(); person.setAge(121); person.setName("zhangsan"); return person; } }
测试注解方式
public class SpringAnnotationTest { public static void main(String[] args) { ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig.class); Person person = applicationContext.getBean(Person.class); System.out.println(person); } }
二 利用注解方式@ComponantScan代替Xml方式的ComponentScan
原先xml方式
<?xml version="1.0" encoding="UTF-8" ?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"> <!--2 xml配置的包扫描 只要标注了@controller @service @Repository @component 任何一个组件--> <context:component-scan base-package="com.melon"></context:component-scan> </beans>
测试方式差不多
现在改成@componantScan方式
package com.melon.config; import com.melon.bean.Person; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.FilterType; import org.springframework.stereotype.Controller; import org.springframework.stereotype.Service; //配置类=配置文件 @Configuration //告诉spring这是一个配置类 @ComponentScan(value = "com.melon", //扫描时候需要排除的组件 excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION,classes ={Controller.class,Service.class})} //includeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION,classes ={Controller.class})} //,useDefaultFilters = false ) //包扫描的根目录 //扫描时候只包含 public class MainConfig { //给容器注册个Bean,类型为返回的类型,id默认是用方法名作为id @Bean public Person person(){ Person person = new Person(); person.setAge(121); person.setName("zhangsan"); return person; } }
测试
package com.melon.config; import com.melon.bean.Person; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class SpringAnnotationTest { public static void main(String[] args) { ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig.class); //Person person = applicationContext.getBean(Person.class); //System.out.println(person); //打印组件 String[] names =applicationContext.getBeanDefinitionNames(); for(String name : names){ System.out.println(name); } } }
相关推荐
面试官:Spring 注解 @After,@Around,@Before 的执行顺序是?.zip 面试官:Spring 注解 @After,@Around,@Before 的执行顺序是?.zip 面试官:Spring 注解 @After,@Around,@Before 的执行顺序是?.zip 面试官:...
包含了 @ComponentScan、@Configuration 和 @EnableAutoConfiguration 注解。 其中 @ComponentScan 让 spring Boot 扫描到 Configuration 类并把它加入到程序上下文。 @Configuration U等同于 spring 的 XML 配置...
注解包含: 拦截器 , 过滤器 , 序列化 , @After , @AfterReturning , @AfterThrowing , @annotation , @Around , @Aspect , @Autowired , @Bean , @Before , @Component , @ComponentScan , @ComponentScans , @...
Spring注解@Component、@Repository、@Service、@Controller区别.doc
主要介绍了详解Spring 注解之@Import 注入的各种花活,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
NULL 博文链接:https://forestqqqq.iteye.com/blog/2235292
Spring注解 @Component、@Repository、@Service、@Controller区别,有兴趣的可能看一下。
赠送jar包:spring-boot-configuration-processor-2.5.6.jar; 赠送原API文档:spring-boot-configuration-processor-2.5.6-javadoc.jar; 赠送源代码:spring-boot-configuration-processor-2.5.6-sources.jar; ...
Spring注解@Resource和@Autowired.doc
赠送jar包:spring-boot-configuration-processor-2.6.3.jar; 赠送原API文档:spring-boot-configuration-processor-2.6.3-javadoc.jar; 赠送源代码:spring-boot-configuration-processor-2.6.3-sources.jar; ...
赠送jar包:spring-boot-configuration-processor-2.3.12.RELEASE.jar; 赠送原API文档:spring-boot-configuration-processor-2.3.12.RELEASE-javadoc.jar; 赠送源代码:spring-boot-configuration-processor-...
主要介绍了spring 注解验证@NotNull等使用方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
主要介绍了Spring @Configuration注解及配置方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
主要介绍了Spring注解之@Lazy注解使用解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
Struts2+spring注解配置 很好哦
本文档介绍了Struts2以及spring的注解配置。
SPring注解及页面跳转实例
主要介绍了详解Spring缓存注解@Cacheable,@CachePut , @CacheEvict使用,非常具有实用价值,需要的朋友可以参考下
@SpringBootApplication: 包含了@ComponentScan、@Configuration和@EnableAutoConfiguration注解。其中@ComponentScan让Spring Boot扫描到Configuration类并把它加入到程序上下文。 @Configuration: 等同于Spring...