spring-注解---autowired
package com.zwj.bean; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; //默认加在ioc容器中的组件,容器启动会调用无参构造器创建对象,再进行初始化赋值等操作 @Component public class Boss { private Car car; //构造器要用的组件,都是从容器中获取 public Boss(Car car){ this.car = car; System.out.println("Boss...有参构造器"); } public Car getCar() { return car; } //@Autowired //标注在方法,Spring容器创建当前对象,就会调用方法,完成赋值; //方法使用的参数,自定义类型的值从ioc容器中获取 public void setCar(Car car) { this.car = car; } @Override public String toString() { return "Boss [car=" + car + "]"; } }
package com.zwj.bean; import org.springframework.stereotype.Component; @Component public class Car { public Car(){ System.out.println("car constructor..."); } public void init(){ System.out.println("car ... init..."); } public void detory(){ System.out.println("car ... detory..."); } }
package com.zwj.bean; public class Color { private Car car; public Car getCar() { return car; } public void setCar(Car car) { this.car = car; } @Override public String toString() { return "Color [car=" + car + "]"; } }
package com.zwj.bean; import org.springframework.beans.BeansException; import org.springframework.beans.factory.BeanNameAware; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.context.EmbeddedValueResolverAware; import org.springframework.stereotype.Component; import org.springframework.util.StringValueResolver; @Component public class Red implements ApplicationContextAware,BeanNameAware,EmbeddedValueResolverAware { private ApplicationContext applicationContext; @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { // TODO Auto-generated method stub System.out.println("传入的ioc:"+applicationContext); this.applicationContext = applicationContext; } @Override public void setBeanName(String name) { // TODO Auto-generated method stub System.out.println("当前bean的名字:"+name); } @Override public void setEmbeddedValueResolver(StringValueResolver resolver) { // TODO Auto-generated method stub String resolveStringValue = resolver.resolveStringValue("你好 ${os.name} 我是 #{20*18}"); System.out.println("解析的字符串:"+resolveStringValue); } }
package com.zwj.config; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import com.zwj.bean.Car; import com.zwj.bean.Color; import com.zwj.dao.BookDao; /** * 自动装配; * Spring利用依赖注入(DI),完成对IOC容器中中各个组件的依赖关系赋值; * * 1)、@Autowired:自动注入: * 1)、默认优先按照类型去容器中找对应的组件:applicationContext.getBean(BookDao.class);找到就赋值 * 2)、如果找到多个相同类型的组件,再将属性的名称作为组件的id去容器中查找 * applicationContext.getBean("bookDao") * 3)、@Qualifier("bookDao"):使用@Qualifier指定需要装配的组件的id,而不是使用属性名 * 4)、自动装配默认一定要将属性赋值好,没有就会报错; * 可以使用@Autowired(required=false); * 5)、@Primary:让Spring进行自动装配的时候,默认使用首选的bean; * 也可以继续使用@Qualifier指定需要装配的bean的名字 * BookService{ * @Autowired * BookDao bookDao; * } * * 2)、Spring还支持使用@Resource(JSR250)和@Inject(JSR330)[java规范的注解] * @Resource: * 可以和@Autowired一样实现自动装配功能;默认是按照组件名称进行装配的; * 没有能支持@Primary功能没有支持@Autowired(reqiured=false); * @Inject: * 需要导入javax.inject的包,和Autowired的功能一样。没有required=false的功能; * @Autowired:Spring定义的; @Resource、@Inject都是java规范 * * AutowiredAnnotationBeanPostProcessor:解析完成自动装配功能; * * 3)、 @Autowired:构造器,参数,方法,属性;都是从容器中获取参数组件的值 * 1)、[标注在方法位置]:@Bean+方法参数;参数从容器中获取;默认不写@Autowired效果是一样的;都能自动装配 * 2)、[标在构造器上]:如果组件只有一个有参构造器,这个有参构造器的@Autowired可以省略,参数位置的组件还是可以自动从容器中获取 * 3)、放在参数位置: * * 4)、自定义组件想要使用Spring容器底层的一些组件(ApplicationContext,BeanFactory,xxx); * 自定义组件实现xxxAware;在创建对象的时候,会调用接口规定的方法注入相关组件;Aware; * 把Spring底层一些组件注入到自定义的Bean中; * xxxAware:功能使用xxxProcessor; * ApplicationContextAware==》ApplicationContextAwareProcessor; * * * @author lfy * */ @Configuration @ComponentScan({"com.zwj.service","com.zwj.dao", "com.zwj.controller","com.zwj.bean"}) public class MainConifgOfAutowired { //@Primary @Bean("bookDao2") public BookDao bookDao(){ BookDao bookDao = new BookDao(); bookDao.setLable("2"); return bookDao; } /** * @Bean标注的方法创建对象的时候,方法参数的值从容器中获取 * @param car * @return */ @Bean public Color color(Car car){ Color color = new Color(); color.setCar(car); return color; } }
package com.zwj.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import com.zwj.service.BookService; @Controller public class BookController { @Autowired private BookService bookService; }
package com.zwj.dao; import org.springframework.stereotype.Repository; //名字默认是类名首字母小写 @Repository public class BookDao { private String lable = "1"; public String getLable() { return lable; } public void setLable(String lable) { this.lable = lable; } @Override public String toString() { return "BookDao [lable=" + lable + "]"; } }
package com.zwj.service; import javax.annotation.Resource; import javax.inject.Inject; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Service; import com.zwj.dao.BookDao; @Service public class BookService { //@Qualifier("bookDao") //@Autowired(required=false) @Resource(name="bookDao2") //@Inject private BookDao bookDao; public void print(){ System.out.println(bookDao); } @Override public String toString() { return "BookService [bookDao=" + bookDao + "]"; } }
package com.zwj.test; import org.junit.Test; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import com.zwj.bean.Boss; import com.zwj.bean.Car; import com.zwj.bean.Color; import com.zwj.config.MainConifgOfAutowired; import com.zwj.service.BookService; public class IOCTest_Autowired { @Test public void test01(){ AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConifgOfAutowired.class); BookService bookService = applicationContext.getBean(BookService.class); System.out.println(bookService); //BookDao bean = applicationContext.getBean(BookDao.class); //System.out.println(bean); Boss boss = applicationContext.getBean(Boss.class); System.out.println(boss); Car car = applicationContext.getBean(Car.class); System.out.println(car); Color color = applicationContext.getBean(Color.class); System.out.println(color); System.out.println(applicationContext); applicationContext.close(); } } /* car constructor... Boss...有参构造器 当前bean的名字:red 解析的字符串:你好 Windows 7 我是 360 传入的ioc:org.springframework.context.annotation.AnnotationConfigApplicationContext@512ddf17: startup date [Sat Oct 27 15:13:01 CST 2018]; root of context hierarchy BookService [bookDao=BookDao [lable=2]] Boss [car=com.zwj.bean.Car@31d7b7bf] com.zwj.bean.Car@31d7b7bf Color [car=com.zwj.bean.Car@31d7b7bf] org.springframework.context.annotation.AnnotationConfigApplicationContext@512ddf17: startup date [Sat Oct 27 15:13:01 CST 2018]; root of context hierarchy */
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.zwj</groupId> <artifactId>spring-annotation---autowire</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <!-- https://mvnrepository.com/artifact/org.springframework/spring-context --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.12.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/junit/junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/javax.inject/javax.inject --> <dependency> <groupId>javax.inject</groupId> <artifactId>javax.inject</artifactId> <version>1</version> </dependency> </dependencies> </project>