@Bean的使用
@Bean是一个方法级别的注解类似于XML的元素,这个注解提供了一些的属性例如 init-method, destroy-method, autowiring 和name
应用场合:@Configuration注解或者@Component注解的类
声明一个Bean
为了声明一个Bean简单的在方法上注册一个@Bean注解,用这种方式在ApplicationContext接口下注册一个具体类型的Bean
@Component//@Configuration
public class BasedJava {
@Bean
public Hello hello(){
return new Hello();
}
}
等同于:
<beans>
<bean id="hello" class="com.hello"/>
</beans>
实现生命周期中的回调函数
所有用@Bean注解的类均支持常规的生命周期回调函数并且可以使用JSR-250标准中的注解,如果一个Bean实现了 InitializingBean DisposableBean,他们相应的方法也会被容器调用
@Bean支持具体的任意的初始化和销毁的回调函数类似于Spring XML中Bean元素上面的init-method和destory-method属性
@Component
public class BasedJava {
@Bean(initMethod = "init",destroyMethod = "destoryBean")
public Hello hello(){
return new Hello();
}
}
使用@Scope指定Bean的作用域
@scope默认为singleton即单例模式,当然也可以更改其值为**多例模式**prototype,在Spring 2.0之后,为支持web应用的ApplicationContext,增强另外三种:request,session和global session类型,它们只实用于web程序,通常是和XmlWebApplicationContext共同使用。后续在Spring MVC的学习中将会再详细介绍。
自定义Bean的名字
@Component
public class BasedJava {
@Bean(initMethod = "init",destroyMethod = "destoryBean",name = "hello")
public Hello hello(){
return new Hello();
}
}
为Bean定义别名(将一个字符串数组赋值给name属性)
@Component
public class BasedJava {
@Bean(initMethod = "init",destroyMethod = "destoryBean",name = {"hello1","hello2","hello3"})
public Hello hello(){
return new Hello();
}
}
为Bean定义添加描述
为Bean添加文字描述可以方便在Bean的管理
@Component
public class BasedJava {
@Bean(initMethod = "init",destroyMethod = "destoryBean",name = {"hello1","hello2","hello3"})
@Description("spring bean ")
public Hello hello(){
return new Hello();
}
}
使用@Configuration注解
@Configuration是一个类级别的注解它用来指示一个对象是Bean定义的来源,@Configuration通过@Bean注释的方法声明Bean,在配置类里面的@Bean注释的方法也可以用来声明内部的Bean依赖,但不可以在@Component注解的类里面使用@Bean声明内部的Bean依赖。(在上篇博文有更加详细的介绍)
附加的Java基础的配置
使用@import注解
正如元素是在Spring XML文件中帮助模块化配置,@Import注解允许从其他的装载类中加载@Bean定义的类
@Component
@Import( BasedJava2.class)
public class BasedJava {
@Bean(initMethod = "init",destroyMethod = "destoryBean",name = {"hello1","hello2","hello3"})
@Description("spring bean ")
public Hello hello(){
return new Hello();
}
}
@Component
public class BasedJava2 {
@Bean
@Description("spring bean ")
public ComponentTest componentTest(){
return new ComponentTest();
}
}
AnnotationConfigApplicationContext appl=new AnnotationConfigApplicationContext(BasedJava.class);
//ComponentTest类的测试
ComponentTest com= appl.getBean(ComponentTest.class);
com.setName("@component");
com.setId(88);
System.out.println(com.toString());
//Hello类的测试
Hello hello= appl.getBean(Hello.class);
hello.setMessage("hello class 测试");
System.out.println(hello.getMessage());
由此看出只要指定明确的了BaseJava类进行处理就可以调用BaseJava2中的bean,而不需要指定BaseJava BaseJava2两个类
@Configuration 下用@ImportResource导入XML信息
在应用中@Configuration 注解类是容器基本的机制,仍然很有必要的去利用一些XML文件,在这些情节中,简单的使用@ImportResource并且定义一些XML文件是需要的。按照这种方式实现以java为中心的配置容器的方法,并且使XML最少
ImportSourceDemo.java
package com.demo.configuration;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
/**
* Created by ruanjianlin on 2017/10/10.
*/
@Configuration
@ImportResource("classpath*:importSource.xml")
public class ImportSourceDemo {
@Value("${JDBC.URL}")
private String url;
@Value("${JDBC.PASSWORD}")
private String password;
@Value("${JDBC.USER}")
private String user;
@Bean
public JdbcConnect jdbcConnect(){
return new JdbcConnect(url,password,user);
}
}
JdbcConnect.java
package com.demo.configuration;
/**
* Created by ruanjianlin on 2017/10/10.
*/
public class JdbcConnect {
private String url;
private String password;
private String user;
public JdbcConnect(String url, String password, String user) {
this.url = url;
this.password = password;
this.user = user;
}
@Override
public String toString() {
return "JdbcConnect{" + "url='" + url + '\'' + ", password='" + password + '\'' + ", user='" + user + '\'' + '}';
}
}
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.xsd">
<context:property-placeholder location="classpath:jdbc.properties"/>
</beans>
Jdbc.properties
jdbc.url=jdbc:mysql:mysql://localhost/spring
jdbc.username=test
jdbc.password=test
注:“classpath”: 用于加载类路径(包括jar包)中的一个且仅一个资源;对于多个匹配的也只返回一个,所以如果需要多个匹配的请考虑“classpath*:”前缀;