spring-註解 (1)
阿新 • • 發佈:2018-10-25
c-c beans books hid implement tin 小寫 span print
spring-註解 (1) -- include/exclude
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..."); }Carpublic void detory(){ System.out.println("car ... detory..."); } }
package com.zwj.bean; import org.springframework.beans.factory.annotation.Value; public class Person { //使用@Value賦值; //1、基本數值 //2、可以寫SpEL; #{} //3、可以寫${};取出配置文件【properties】中的值(在運行環境變量裏面的值) @Value(Persion"張三") private String name; @Value("#{20-2}") private Integer age; @Value("${person.nickName}") private String nickName; public String getNickName() { return nickName; } public void setNickName(String nickName) { this.nickName = nickName; }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; } public Person(String name, Integer age) { super(); this.name = name; this.age = age; } public Person() { super(); // TODO Auto-generated constructor stub } @Override public String toString() { return "Person [name=" + name + ", age=" + age + ", nickName=" + nickName + "]"; } }
package com.zwj.config; 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 com.zwj.bean.Person; import com.zwj.service.BookService; import org.springframework.context.annotation.ComponentScan.Filter; import org.springframework.context.annotation.ComponentScans; //配置類==配置文件 /*@ComponentScans( value = { @ComponentScan(value="com.atguigu",includeFilters = { @Filter(type=FilterType.ANNOTATION,classes={Controller.class}), @Filter(type=FilterType.ASSIGNABLE_TYPE,classes={BookService.class}), @Filter(type=FilterType.CUSTOM,classes={MyTypeFilter.class}) },useDefaultFilters = false) } )*/ //@ComponentScan value:指定要掃描的包 //excludeFilters = Filter[] :指定掃描的時候按照什麽規則排除那些組件 //includeFilters = Filter[] :指定掃描的時候只需要包含哪些組件 //FilterType.ANNOTATION:按照註解 //FilterType.ASSIGNABLE_TYPE:按照給定的類型; //FilterType.ASPECTJ:使用ASPECTJ表達式 //FilterType.REGEX:使用正則指定 //FilterType.CUSTOM:使用自定義規則 @ComponentScans( value = { @ComponentScan(value="com.zwj",excludeFilters= { //指定某一個註解@controller/@repository/@compnement/@service ,不加入到IOC容器 @Filter(type=FilterType.ANNOTATION,classes= {Controller.class}), // 指定具體某一個類,不註入到IOC容器裏 @Filter(type=FilterType.ASSIGNABLE_TYPE,classes={BookService.class}) }) } ) @Configuration //告訴Spring這是一個配置類 public class MainConfigExclude { //給容器中註冊一個Bean;類型為返回值的類型,id默認是用方法名作為id @Bean("person") public Person person01(){ return new Person("lisi", 20); } }MainConfigExclude
package com.zwj.config; 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 com.zwj.bean.Person; import com.zwj.service.BookService; import org.springframework.context.annotation.ComponentScan.Filter; import org.springframework.context.annotation.ComponentScans; @ComponentScans( value = { @ComponentScan(value="com.zwj",includeFilters = { @Filter(type=FilterType.ANNOTATION,classes={Controller.class}), @Filter(type=FilterType.ASSIGNABLE_TYPE,classes={BookService.class}), // 自定義過濾規則 @Filter(type=FilterType.CUSTOM,classes={MyTypeFilter.class}) //關閉默認,默認是加載所有的@controller/service/repository/component },useDefaultFilters = false) } ) @Configuration //告訴Spring這是一個配置類 public class MainConfigInclude { //給容器中註冊一個Bean;類型為返回值的類型,id默認是用方法名作為id @Bean("person") public Person person01(){ return new Person("lisi", 20); } }MainConfigInclude
package com.zwj.config; import java.io.IOException; import org.springframework.core.io.Resource; import org.springframework.core.type.AnnotationMetadata; import org.springframework.core.type.ClassMetadata; import org.springframework.core.type.classreading.MetadataReader; import org.springframework.core.type.classreading.MetadataReaderFactory; import org.springframework.core.type.filter.TypeFilter; public class MyTypeFilter implements TypeFilter { /** * metadataReader:讀取到的當前正在掃描的類的信息 * metadataReaderFactory:可以獲取到其他任何類信息的 */ public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException { // TODO Auto-generated method stub //獲取當前類註解的信息 AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata(); //獲取當前正在掃描的類的類信息 ClassMetadata classMetadata = metadataReader.getClassMetadata(); //獲取當前類資源(類的路徑) Resource resource = metadataReader.getResource(); String className = classMetadata.getClassName(); System.out.println("--->"+className); if(className.contains("er")){ return true; } return false; } }MyTypeFilter
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; }BookController
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 + "]"; } }BookDao
package com.zwj.service; import javax.annotation.Resource; 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*/ @Autowired(required=false) private BookDao bookDao; public void print(){ System.out.println(bookDao); } @Override public String toString() { return "BookService [bookDao=" + bookDao + "]"; } }BookService
package com.zwj.test; import static org.junit.Assert.*; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import com.sun.xml.internal.ws.addressing.v200408.MemberSubmissionAddressingConstants; import com.zwj.bean.Person; import com.zwj.config.MainConfigExclude; import com.zwj.config.MainConfigInclude; public class IocTest { @Test public void testExclude() { AnnotationConfigApplicationContext acac = new AnnotationConfigApplicationContext(MainConfigExclude.class); // 取出所有註入到IOC容器的key 的名字 String[] names = acac.getBeanDefinitionNames(); for (String string : names) { System.out.println(string); } // 取出IOC容器中的對象 Person person = acac.getBean(Person.class); System.out.println(person); } /* mainConfigExclude car mainConfigInclude bookDao person myTypeFilter bookController bookService Person [name=張三, age=18, nickName=${person.nickName}] */ @Test public void testInclude() { AnnotationConfigApplicationContext acac = new AnnotationConfigApplicationContext(MainConfigInclude.class); // 取出所有註入到IOC容器的key 的名字 String[] names = acac.getBeanDefinitionNames(); for (String string : names) { System.out.println(string); } // 取出IOC容器中的對象 Person person = acac.getBean(Person.class); System.out.println(person); } /* mainConfigInclude person myTypeFilter bookController bookService Person [name=張三, age=18, nickName=${person.nickName}] */ }IocTest
<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-config</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.12.RELEASE</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies> </project>pom.xml
github 地址 :https://github.com/typecone/spring-annotation.git
spring-註解 (1)