Spring 注解篇
一、注解
1.用于創(chuàng)建對象的注解
定義:是指由一個(gè)工廠對象決定創(chuàng)建出哪一種產(chǎn)品類
提示:這些注解的作用和在xml中配置標(biāo)簽的作用一樣。
● @Component
作用: 用于將當(dāng)前類對象存入Spring容器中
屬性:value屬性用于指定存入spring容器中的bean的id,不配置時(shí)默認(rèn)為:當(dāng)前類的類名首字母轉(zhuǎn)小寫
● @Controller
一般用在表現(xiàn)層。
作用、屬性同@Component完全一樣。
● @Service
一般用在業(yè)務(wù)層。
作用、屬性同@Component完全一樣。
● @Repository
一般用在持久層。
作用、屬性同@Component完全一樣。
提示:@Controller、@Service、@Repository三個(gè)注解的作用、屬性同@Component完全一樣
2.用于注入數(shù)據(jù)的注解
這些注解的作用和xml配置的標(biāo)簽下的標(biāo)簽的作用是一樣的。
@Autowired、@Qualifier、@Resource三個(gè)注解只能注入bean類型的數(shù)據(jù),不能注入基本數(shù)據(jù)類型和String類型。
● @Autowired
作用:
自動(dòng)按照類型注入,只要容器中有唯一的一個(gè)bean對象類型和要注入的變量類型匹配,就可以注入成功。
如果IoC容器中沒有任何bean的類型和要注入的變量的類型匹配,則報(bào)錯(cuò)。
如果IoC容器中該類型的bean存在多個(gè),則將要注入的變量的變量名作為bean的id進(jìn)行二次匹配:
如果根據(jù)變量名可以找到唯一的bean,則進(jìn)行注入。
如果根據(jù)變量名匹配不到,則報(bào)錯(cuò)。
出現(xiàn)位置:可以使變量上,也可以是方法上。
細(xì)節(jié):在使用注解進(jìn)行注入時(shí),變量的setter方法就不是必須的了。
@Autowired有一個(gè)屬性required,默認(rèn)為true。如果設(shè)置為false,則該對象可以在獲取不到bean時(shí)默認(rèn)為null。
- 標(biāo)注在屬性上
public class Boss {
@Autowired
private Car car;
}
- 標(biāo)注在方法上
Spring容器創(chuàng)建當(dāng)前對象,就會(huì)調(diào)用方法,完成賦值。
方法使用的參數(shù),自定義類型的值從ioc容器中獲取。
示例:
@Autowired
public void setCar(Car car) {
this.car = car;
}
- 標(biāo)注在有參構(gòu)造器上
默認(rèn)加在ioc容器中的組件,容器啟動(dòng)會(huì)調(diào)用無參構(gòu)造器創(chuàng)建對象,再進(jìn)行初始化賦值等操作。
給bean移除無參構(gòu)造器,添加一個(gè)有參構(gòu)造器,為該有參構(gòu)造器添加@Autowired注解。Spring容器啟動(dòng)時(shí)會(huì)調(diào)用該有參構(gòu)造器,并從ioc容器中獲取參數(shù)對應(yīng)類型的bean對象進(jìn)行注入。
@Autowired
public Boss(Car car) {
this.car = car;
}
- 標(biāo)注在構(gòu)造器參數(shù)位置
public Boss(@Autowired Car car) {
this.car = car;
}
如果當(dāng)前類只有一個(gè)有參構(gòu)造器,Spring容器創(chuàng)建該bean只能調(diào)用該有參構(gòu)造器,有參構(gòu)造器的@Autowired可以省略,參數(shù)依然可以從ioc容器中獲?。?br> 使用構(gòu)造函數(shù)進(jìn)行注入時(shí),需要標(biāo)注final來表示這個(gè)注入的變量不能被改變。
在@Configuration類中通過@Bean將Color加入容器,并使用@Autowired注入Car:
● @Qualifier
作用:
在按照類型匹配的基礎(chǔ)上,再按照名稱匹配注入。
它在給類的成員變量注入時(shí),不能單獨(dú)使用,要和@Autowired配合使用。當(dāng)按照類型注入沖突時(shí)配合@AutoWired
它在給方法參數(shù)進(jìn)行注入時(shí),可以單獨(dú)使用。
屬性:
value:用于指定要注入的bean的id。
● @Resource
作用:直接按照bean的id進(jìn)行注入。它可以獨(dú)立使用。
屬性:
name:用于指定bean的id。
● @Value
作用:用于注入基本類型和String類型的變量
屬性:
value:用于指定數(shù)據(jù)的值??梢耘渲茫鹤置媪?、${key}(從環(huán)境變量、配置文件中獲取值)、使用Spring中的SpEL(Spring中的EL表達(dá)式:#{表達(dá)式})。
public class Person {
@Value("張三")
private String name;
@Value("#{ 10 + 8} ")
private Integer age;
@Value("${person.nickName}")
private String nickName;
// getter/setter方法
}
●@Inject
和@Autowired功能類似,但是沒有required屬性。
需要導(dǎo)入包
<dependency>
<gourpId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
3.用于改變作用范圍的注解
提示:這些注解的作用和xml配置的標(biāo)簽的scope屬性的作用一樣。
●@Scope
作用:用于指定bean的作用范圍。
屬性:
value:指定范圍的取值。常用取值:singleton、prototype。(不配置時(shí)默認(rèn)為singleton)
● value
取值:
● ConfigurableBeanFactory.SCOPE_SINGLETON : “singleton”,單實(shí)例(默認(rèn)值),ioc容器啟動(dòng)就會(huì)調(diào)用方法創(chuàng)建對象到ioc容器中。
● ConfigurableBeanFactory.SCOPE_PROTOTYPE : “prototype”,多實(shí)例,ioc容器啟動(dòng)時(shí)并不會(huì)調(diào)用方法創(chuàng)建對象,每次獲取時(shí)才會(huì)調(diào)用方法創(chuàng)建對象。
● WebApplicationContext.SCOPE_REQUEST:“request”,同一個(gè)請求創(chuàng)建一個(gè)實(shí)例
● WebApplicationContext.SCOPE_SESSION:“session”,同一個(gè)session創(chuàng)建一個(gè)實(shí)例
● scopeName
同’value’
●@Profile
Spring為我們提供的可以根據(jù)當(dāng)前環(huán)境,動(dòng)態(tài)的激活和切換一系列Bean的功能。
指定組件在哪個(gè)環(huán)境的情況下才能被注冊到容器中,不指定時(shí)在任何環(huán)境下都能注冊這個(gè)組件。
加了@Profile環(huán)境標(biāo)識(shí)的bean,只有這個(gè)環(huán)境被激活的時(shí)候才能被注冊到容器中。默認(rèn)是default環(huán)境。
例如:
@Profile("default")
@Bean("defaultDataSource")
public DataSource dataSourceDefault(@Value("${db.password}") String pwd) throws Exception{
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setUser(user);
dataSource.setPassword(pwd);
dataSource.setJdbcUrl("jdbc:oracle:thin:@localhost:1521:orcl");
dataSource.setDriverClass(driverClass);
return dataSource;
}
@Profile("test") // value可以隨便自定義
@Bean("color")
public Color color() {
return new Color();
}
@Profile("test")
@Bean("testDataSource")
public DataSource dataSourceTest(@Value("${db.password}") String pwd) throws Exception{
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setUser(user);
dataSource.setPassword(pwd);
dataSource.setJdbcUrl("jdbc:oracle:thin:@localhost:1521:orcl");
dataSource.setDriverClass(driverClass);
return dataSource;
}
@Profile("dev")
@Bean("devDataSource")
public DataSource dataSourceDev(@Value("${db.password}")String pwd) throws Exception{
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setUser(user);
dataSource.setPassword(pwd);
dataSource.setJdbcUrl("jdbc:oracle:thin:@localhost:1521:orcl");
dataSource.setDriverClass(driverClass);
return dataSource;
}
切換激活profile環(huán)境:(不切換時(shí),系統(tǒng)默認(rèn)為default環(huán)境)
使用 JVM 命令行參數(shù)
添加 VM arguments:-Dspring.profiles.active=test,便可以將環(huán)境切換到test環(huán)境
使用ioc無參構(gòu)造器,激活環(huán)境配置
如果使用有參構(gòu)造器AnnotationConfigApplicationContext(Class<?>… annotatedClasses)獲取ioc容器,該構(gòu)造函數(shù)中會(huì)執(zhí)行以下代碼:
可以使用無參構(gòu)造器手動(dòng)調(diào)用這些方法,實(shí)現(xiàn)這個(gè)效果。同時(shí),可以在注冊配置類之前,切換環(huán)境配置,防止把配置類中所有配置全部加載:
通過無參構(gòu)造器獲取ioc容器
通過ioc容器的environment對象切換環(huán)境(可以同時(shí)配置多個(gè)環(huán)境)
調(diào)用register注冊主配置類
調(diào)用refresh加載bean
public AnnotationConfigApplicationContext(Class<?>... annotatedClasses) {
this();
register(annotatedClasses);
refresh();
}
@Test
public void test01() {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
applicationContext.getEnvironment().setActiveProfiles("dev", "test");
applicationContext.register(MainConfigOfProfile.class);
applicationContext.refresh();
String[] beans = applicationContext.getBeanNamesForType(DataSource.class);
Arrays.asList(beans).forEach(System.out::println);
}
@Profile也可以注解在配置類上:只有在指定的環(huán)境的時(shí)候,整個(gè)配置類里面的所有配置才能生效。
如果此時(shí)激活的環(huán)境為dev:雖然devDataSource這個(gè)bean上配置的環(huán)境是dev,但是因?yàn)檎麄€(gè)配置類的環(huán)境是test,所以整個(gè)配置類都不進(jìn)行加載,devDataSource也不會(huì)進(jìn)行加載。
@Profile("test")
@PropertySource("classpath:/dbconfig.properties")
@Configuration
public class MainConfigOfProfile implements EmbeddedValueResolverAware {
@Value("${db.user}")
private String user;
private StringValueResolver resolver;
private String driverClass;
@Profile("default")
@Bean("defaultDataSource")
public DataSource dataSourceDefault(@Value("${db.password}") String pwd) throws Exception{
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setUser(user);
dataSource.setPassword(pwd);
dataSource.setJdbcUrl("jdbc:oracle:thin:@localhost:1521:orcl");
dataSource.setDriverClass(driverClass);
return dataSource;
}
@Profile("test")
@Bean("testDataSource")
public DataSource dataSourceTest(@Value("${db.password}") String pwd) throws Exception{
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setUser(user);
dataSource.setPassword(pwd);
dataSource.setJdbcUrl("jdbc:oracle:thin:@localhost:1521:orcl");
dataSource.setDriverClass(driverClass);
return dataSource;
}
@Profile("dev")
@Bean("devDataSource")
public DataSource dataSourceDev(@Value("${db.password}")String pwd) throws Exception{
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setUser(user);
dataSource.setPassword(pwd);
dataSource.setJdbcUrl("jdbc:oracle:thin:@localhost:1521:orcl");
dataSource.setDriverClass(driverClass);
return dataSource;
}
@Profile("prod")
@Bean("prodDataSource")
public DataSource dataSourceProd(@Value("${db.password}")String pwd) throws Exception{
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setUser(user);
dataSource.setPassword(pwd);
dataSource.setJdbcUrl("jdbc:oracle:thin:@localhost:1521:orcl");
dataSource.setDriverClass(driverClass);
return dataSource;
}
@Override
public void setEmbeddedValueResolver(StringValueResolver resolver) {
this.resolver = resolver;
this.driverClass = resolver.resolveStringValue("${db.driverClass}");
}
}
4.和生命周期相關(guān)的注解
提示:這些注解的作用和xml配置的標(biāo)簽的init-method、destroy-method屬性作用相同。
● @PreDestroy
作用:用于指定銷毀方法。
● @PostConstruct
作用:用于指定初始化方法。
@Service("accountService")
@Scope("prototype")
public class AccountServiceImpl implements IAccountService {
@Value("${server.port}")
private String port;
@Resource(name = "accountDao")
private IAccountDao accountDao;
@Autowired
@Qualifier("hAccountDao")
private HAccountDao hAccountDao;
public void saveAccount() {
accountDao.saveAccount();
}
@PostConstruct
public void init() {
System.out.println("初始化方法");
}
@PreDestroy
public void destroy() {
System.out.println("銷毀方法");
}
}
● @Lazy
作用:針對單實(shí)例的Bean。單實(shí)例Bean默認(rèn)在容器啟動(dòng)時(shí)創(chuàng)建對象。懶加載在第一次使用Bean時(shí)才創(chuàng)建對象并初始化。
@Bean
@Lazy
public Person person() {
System.out.println("方法調(diào)用了。");
return new Person("ZhangSan", 10);
}
● @Conditional
作用:照條件進(jìn)行判斷,滿足條件才給容器中注冊Bean。該注解在Spring、SpringBoot底層中經(jīng)常使用
value屬性需要傳入一個(gè)判斷條件,該判斷條件是一個(gè)實(shí)現(xiàn)了Condition接口的類。
自定義判斷條件類
public class MyCondition implements Condition {
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
// 獲取類加載器
ClassLoader classLoader = context.getClassLoader();
// 獲取ioc使用的beanFactory
ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
// 獲取到bean定義的注冊類
BeanDefinitionRegistry registry = context.getRegistry();
ResourceLoader resourceLoader = context.getResourceLoader();
// 獲取當(dāng)前環(huán)境信息
Environment environment = context.getEnvironment();
String osName = environment.getProperty("os.name");
return osName.startsWith("Linux");
}
}
測試小技巧:
os.name是獲取當(dāng)前系統(tǒng),默認(rèn)取的當(dāng)前系統(tǒng)的系統(tǒng)名稱,但是我們可以在 JVM 參數(shù)上強(qiáng)制給一個(gè)值。
VM options: -Dos.name=linux
在bean上加上判斷條件
@Bean("person01")
@Conditional(MyCondition.class)
public Person person01() {
return new Person("lisi", 18);
}
該注解也可以用在類上,對類中所有bean統(tǒng)一設(shè)置:
@Configuration
@Conditional(MyCondition.class)
public class MyConfig {
@Bean("person")
public Person person() {
return new Person("ZhangSan", 10);
}
@Bean("person01")
public Person person01() {
return new Person("lisi", 18);
}
}
5.在配置類中利用java方法創(chuàng)建bean
提示:當(dāng)我們需要在程序中使用commons-dbutils去操作數(shù)據(jù)庫時(shí),需要?jiǎng)?chuàng)建DataSource、QueryRunner對象存入Spring的容器。創(chuàng)建對象、存入容器的過程可以放在一個(gè)配置類中,將創(chuàng)建對象的方法返回值作為bean存入容器。
設(shè)置配置類的不同方式:
- 在AnnotationConfigApplicationContext構(gòu)造方法中傳參:
- 使用@Configuration注解聲明配置類:(需要在主配置類中使用@ComponentScan注解掃描到該配置類所在的包)
- 使用@Import注解導(dǎo)入其他配置
● @Configuration
作用:指定當(dāng)前類為一個(gè)配置類
細(xì)節(jié):當(dāng)該類作為AnnotationConfigApplicationContext對象創(chuàng)建的參數(shù)時(shí),該注解可以不寫。
● @ComponentScan
作用:指定Spring在創(chuàng)建容器時(shí)要掃描的包。作用和xml中配置的context:component-scan一樣。
屬性:value/basePackages:兩個(gè)屬性的作用一樣,都是指定創(chuàng)建容器時(shí)要掃描的包。屬性的值為數(shù)組。
● value
要掃描的包,等同于basePackages
● basePackages
同value
● includeFilters
指定只包含的組件
屬性值是一個(gè)@Filter數(shù)組
● excludeFilters
指定排除的組件。
屬性值是一個(gè)@Filter數(shù)組
示例:掃描com.study包下的組件,排除@Controller、@Service注解的類
@Configuration
@ComponentScan(value = "com.study", excludeFilters = {
@ComponentScan.Filter(type = FilterType.ANNOTATION, classes = {Controller.class, Service.class})
})
public class MyConfig {
@Bean
public Person person() {
return new Person("ZhangSan", 10);
}
}
示例:只包含@Controller、@Service注解的類。(需要先禁用默認(rèn)的過濾規(guī)則useDefaultFilters=false,因?yàn)槟J(rèn)的過濾規(guī)則是全部掃描)
@Configuration
@ComponentScan(value = "com.study",
useDefaultFilters = false,
includeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION, classes = {Controller.class, Service.class})
})
public class MyConfig {
@Bean
public Person person() {
return new Person("ZhangSan", 10);
}
}
● @ComponentScans
作用:指定Spring在創(chuàng)建容器時(shí)要掃描的包。作用和xml中配置的context:component-scan一樣。
屬性:value/basePackages:兩個(gè)屬性的作用一樣,都是指定創(chuàng)建容器時(shí)要掃描的包。屬性的值為數(shù)組。
@ComponentScan使用了 JDK8的@Repeatable的注解,表示這個(gè)注解可以在一個(gè)類上多次使用。
如果使用的 JDK7,則可以使用@ComponentScans,value屬性為 @ComponentScan數(shù)組。
● @Bean
作用:將方法的返回值作為一個(gè)bean對象,存入Spring的IoC容器中。
屬性:value/name:兩個(gè)屬性的作用一樣,用于指定bean的id。不配置該屬性時(shí),默認(rèn)以方法名作為id。
細(xì)節(jié):當(dāng)我們給方法配置@Bean注解時(shí),如果方法有參數(shù),Spring會(huì)去容器中查找有無可用的bean對象。查找的方式和@Autowired相同。
●@ComponentScan.Filter
作用:配置包掃描的過濾規(guī)則
@Filter
type
要排除的方式:FilterType。
Annotation:按照注解
Assignable_type:按照給定的類
AspectJ:使用AspectJ表達(dá)式
Regex:使用正則表達(dá)式
Custom:使用自定義規(guī)則,該規(guī)則必須是TypeFilter的實(shí)現(xiàn)類
value
要排除的類型,同classes
classes
同value
pattern
正則表達(dá)式
自定義TypeFilter規(guī)則:
MetadataReader:讀取到的當(dāng)前正在掃描的類的信息
MetadataReaderFactory:可以獲取到其他任何類的信息
實(shí)例:
public class MyTypeFilter implements TypeFilter {
@Override
public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException {
// 獲取當(dāng)前正在掃描類的注解信息
AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata();
// 獲取當(dāng)前正在掃描的類的類信息
ClassMetadata classMetadata = metadataReader.getClassMetadata();
// 獲取當(dāng)前的資源信息
metadataReader.getResource();
String className = classMetadata.getClassName();
return "com.study.book.dao.BookDao".equals(className);
}
}
配置:
@Configuration
@ComponentScan(value = "com.study",
useDefaultFilters = false,
includeFilters = {
@ComponentScan.Filter(type = FilterType.CUSTOM, classes = {MyTypeFilter.class})
})
public class MyConfig {
// ...
}
● @Import
作用:用于導(dǎo)入其他的配置類。
屬性:value:用于指定要導(dǎo)入的其他配置類的字節(jié)碼文件。當(dāng)使用@Import的注解之后,有@Import注解的就是主配置類,而導(dǎo)入的都是子配置類。
使用方式:
@Configuration
public class JdbcConfiguration {
// ....
}
@Configuration
@ComponentScan("com.study")
@Import(JdbcConfiguration .class)
public class SpringConfiguration {
@Bean("runner")
@Scope("prototype") // Spring容器的bean默認(rèn)為單例,為避免不同數(shù)據(jù)庫操作之間的干擾,此處應(yīng)該使用Scope將runner指定為多例
public QueryRunner createQueryRunner(DataSource dataSource) {
return new QueryRunner(dataSource);
}
@Bean("dataSource")
public DataSource createDataSource() {
try {
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setDriverClass("oracle.jdbc.driver.OracleDriver");
dataSource.setJdbcUrl("jdbc:oracle:thin:@127.0.0.1:1521/orcl");
dataSource.setUser("springtest");
dataSource.setPassword("tiger");
return dataSource;
} catch (PropertyVetoException e) {
throw new RuntimeException(e);
}
}
}
@Test
public void testFindAll(){
// 讀取配置類中的配置(如果有多個(gè)配置類,也可以傳入多個(gè)配置類)
ApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfiguration.class);
IAccountService as = ac.getBean("accountService", IAccountService.class);
as.findAllAccount().forEach(System.out::println);
}
給容器中注冊組件:
● 包掃描+組件標(biāo)注注解(@Controller、@Service、@Repository、@Component)[自己寫的類]
● @Bean [導(dǎo)入的第三方包里面的組件]
● @Import[快速的給容器中導(dǎo)入一個(gè)組件]
● 使用Spring提供的FactoryBean(工廠bean)
自定義ImportSelector:
AnnotationMetadata:當(dāng)前標(biāo)注@Import注解的類的所有注解信息。
返回值不能是null,否則會(huì)報(bào)空指針異常。如果需要返回的內(nèi)容,則需使用:return new String[0]。
public class MyImportSelector implements ImportSelector {
@Override
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
return new String[]{"com.study.bean.Yellow", "com.study.bean.Blue"};
}
}
自定義ImportBeanDefinitionRegistrar:
AnnotationMetadata:當(dāng)前類的注解信息
BeanDefinitionRegistry:BeanDefinition注冊類。需要添加到容器中的Bean,可以通過BeanDefinitionRegistry.registerBeanDefinition手工注冊進(jìn)來。
public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
String[] beanDefinitionNames = registry.getBeanDefinitionNames();
Arrays.asList(beanDefinitionNames).forEach(System.out::println);
// 判斷ioc容器中是否注冊了指定id的Bean
boolean redDefinition = registry.containsBeanDefinition("com.study.bean.Red");
boolean blueDefinition = registry.containsBeanDefinition("com.study.bean.Blue");
if(redDefinition && blueDefinition) {
// 調(diào)用registry的registerBeanDefinition方法手工注冊Bean
// 使用RootBeanDefinition指定要注冊的Bean的類型
RootBeanDefinition beanDefinition = new RootBeanDefinition(RainBow.class);
// 指定注冊進(jìn)容器的bean的id
registry.registerBeanDefinition("rainBow", beanDefinition);
}
}
}
配置示例:
@Configuration
@Conditional(MyCondition.class)
@Import({Color.class, Red.class, MyImportSelector.class}) // 可以同時(shí)配置要導(dǎo)入的類名、ImportSelector、ImportBeanDefinitionRegistrar
public class MyConfig {
@Bean("person")
public Person person() {
return new Person("ZhangSan", 10);
}
}
推薦博文:https://blog.csdn.net/qq_15719169/article/details/119252781
6.從properties配置文件中獲取配置
提示: 使用@PropertySource指定讀取的配置文件路徑:
@PropertySource
是一個(gè)可重復(fù)使用的注解。
●@PropertySource的value屬性可以傳多個(gè)路徑。
或者使用@PropertySources指定多個(gè)@PropertySource
編寫jdbcConfig.properties配置文件:
jdbc.driver=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@127.0.0.1:1521/orcl
jdbc.username=springtest
jdbc.password=tiger
在JdbcConfig.java中獲取properties中配置的信息
@Configuration
@ComponentScan({"com.study","com.config"})
@PropertySource("classpath:jdbcConfig.properties")
public class SpringConfiguration {
}
在JdbcConfig.java中獲取properties文件中的配置信息
@Configuration
public class JdbcConfig {
@Value("${jdbc.driver}")
private String jdbcDriver;
@Value("${jdbc.url}")
private String jdbcUrl;
@Value("${jdbc.username}")
private String jdbcUser;
@Value("${jdbc.password}")
private String jdbcPassword;
@Bean("dataSource")
public DataSource createDataSource() {
try {
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setDriverClass(jdbcDriver);
dataSource.setJdbcUrl(jdbcUrl);
dataSource.setUser(jdbcUser);
dataSource.setPassword(jdbcPassword);
return dataSource;
} catch (PropertyVetoException e) {
throw new RuntimeException(e);
}
}
}
二、接口
1.FactoryBean接口
Spring提供的 工廠Bean接口
自定義FactoryBean,用來生成Color的Bean:
public class ColorFactoryBean implements FactoryBean<Color> {
// 返回一個(gè)color對象,這個(gè)對象會(huì)添加進(jìn)容器中
@Override
public Color getObject() throws Exception {
return new Color();
}
// 返回的對象的類型
@Override
public Class<?> getObjectType() {
return Color.class;
}
// 是否為單例:true代表這個(gè)bean是單實(shí)例的,false代表每次獲取都會(huì)創(chuàng)建一個(gè)新的bean
@Override
public boolean isSingleton() {
return true;
}
}
添加該FactoryBean為Bean:
public class ColorFactoryBean implements FactoryBean<Color> {
// 返回一個(gè)color對象,這個(gè)對象會(huì)添加進(jìn)容器中
@Override
public Color getObject() throws Exception {
return new Color();
}
// 返回的對象的類型
@Override
public Class<?> getObjectType() {
return Color.class;
}
// 是否為單例:true代表這個(gè)bean是單實(shí)例的,false代表每次獲取都會(huì)創(chuàng)建一個(gè)新的bean
@Override
public boolean isSingleton() {
return true;
}
}
在獲取colorTest這個(gè)Bean時(shí),實(shí)際獲取到的bean對象的類型是ColorFactoryBean::getObjectType()返回的類型,即Color類型。獲取到的Bean對象是ColorFactoryBean::getObject()創(chuàng)建的對象。
如果想要獲取ColorFactoryBean類型的bean對象,需要在BeanID前添加前綴&,即&colorTest。這個(gè)前綴是在BeanFactory.FACTORY_BEAN_PREFIX中定義的。
2.Aware接口
自定義組件想要使用Spring容器底層的一些組件(例如ApplicationContext、BeanFactory、BeanName等),實(shí)現(xiàn)xxxAware接口,在創(chuàng)建對象的時(shí)候,會(huì)調(diào)用接口規(guī)定的方法,注入相應(yīng)的組件。
xxxAware的總接口為Aware接口。xxxAware的對應(yīng)的解析為xxxAwarePropressor。
示例:
在Bean中注入ioc容器ApplicationContext,實(shí)現(xiàn)ApplicationContextAware接口文章來源:http://www.zghlxwxcb.cn/news/detail-477019.html
public class Car implements ApplicationContextAware {
private ApplicationContext applicationContext;
public Car() {
System.out.println("Car constructor...");
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
System.out.println("為car注入applicationContext");
this.applicationContext = applicationContext;
}
}
String值解析器(可以解析String、SpEL表達(dá)式、環(huán)境變量值、配置文件值):文章來源地址http://www.zghlxwxcb.cn/news/detail-477019.html
public class Person implements EmbeddedValueResolverAware {
@Override
public void setEmbeddedValueResolver(StringValueResolver resolver) {
String str = resolver.resolveStringValue("test: #{ 10 * 2} ==== ${person.nickName} ==== ${os.name} .");
System.out.println(str);
}
}
到了這里,關(guān)于Spring 常用注解篇的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!