spring-註解---autowiredjava
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>