目录
SpringIOC容器对Bean管理
一.bean实例化
1.通过构造方法(默认)
2.通过工厂方法
3.通过静态工厂方法
public class Student {
public Student() {
System.out.println("===》执行无参构造方法");
}
}
public class BeansFactory {
public Student createStu(){
System.out.println("===》执行普通工厂");
return new Student();
}
}
public class StaticBeansFactory {
public static Student createStu(){
System.out.println("===》执行static工厂");
return new Student();
}
}
<--==========================bean的实例化方式1(构造方法)-->
<bean id="student" class="com.apesource.pojo.Student"></bean>
<--==========================bean的实例化方式2(工厂方法)-->
<bean id="student" class="com.apesource.pojo.Student" factory-bean="factory" factory-method="createStu"></bean>
<bean id="factory" class="com.apesource.factory.BeansFactory"></bean>
<--==========================bean的实例化方式3(静态工厂方法)-->
<bean id="student" class="com.apesource.factory.StaticBeansFactory" factory-method="createStu"></bean>
二.bean作用域
含义:spring对于创建javaBean实例的方式
语法:<bean scope="属性值"></bean>
属性值:
singleton=====>单例(默认)
prototype=====>多例
request=======>一个请求创建一个
session=======>一个会话创建一个
public class Teacher {
}
<!--==========================bean的作用域-->
<bean id="teacher" class="com.apesource.pojo.Teacher" scope="prototype"></bean>
三.bean生命周期
1.实例化
2.属性赋值(DI)
3.初始化
3.1接口 InitializingBean
3.2属性 init-method=“”
4.操作使用
5.销毁了
5.1接口 DisposableBean
5.2属性 destory-method=“”
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
public class User implements InitializingBean, DisposableBean {
private String uname;
public void setUname(String uname) {
System.out.println("bean生命周期===》属性赋值");
this.uname = uname;
}
public User() {
System.out.println("bean生命周期===》实例化");
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("bean生命周期===>初始化(接口)");
}
public void doInit(){
System.out.println("bean生命周期===>初始化(属性)");
}
@Override
public void destroy() throws Exception {
System.out.println("bean生命周期===》销毁(接口)");
}
public void doDestory(){
System.out.println("bean声明周期===》销毁(属性)");
}
}
<!--==========================bean的声明周期-->
<bean id="user" class="com.apesource.pojo.User" init-method="doInit" destroy-method="doDestory">
<property name="uname" value="小王"></property>
</bean>
spring的配置
1.spring2.5前==xml
2.spring2.5后==xml+annotation
3.spring3.0后==annotation+JavaConfig配置类
spring2.5后=xml+annotation
目的优化一下代码:
<bean id="" class="" init-method="" destroy-method="" scope="" autowire="">
<property></property>
<constructor-arg></constructor-arg>
</bean>
注解:
1.注入类
替换:<bean id="" class=""></bean>
位置:类
语法:
@Component
eg:Class User{}
<bean id="user" class="com.apesource.包.User"></bean>
||等价于||
@Component
Class User{}
语法:@Component(value="注入容器中的id,如果省略id为类名且首字母小写,value属性名称可以省略")
<context:component-scan base-package=""></context:component-scan>功能一致
含义:扫描所有被@Component注解所修饰的类,注入容器
@Repository=====>注入数据访问层
@Service========>注入业务层
@Controller=====>注入控制层
以上三个注解与@Component功能语法一致
2.注入基本数据
@Value
含义:注入基本数据
替换:<property></property>
修饰:成员变量或对应的set方法
语法:@Value("数据内容")
@Value("${动态获取}")
<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
@Autowired
语法:@Autowired(required = "true-默认、false、是否必须进行装配")
修饰:成员变量或对应的构造方法
含义:按照通过set方法进行“类型装配”,set方法可以省略
注意:
1.默认是按照类型装配且同set方法
2.若容器中有一个类型可以与之匹配则装配成功,若没有一个类型可以匹配则报错
NoSuchBeanDefinitionException
3.若容器中有多个类型可以与之匹配,则自动切换为按照名称装配,若名称没有对应,则报错 NoUniqueBeanDefinitionException
message.properties:
msg1=光头强
msg2=180
@Component(value = "stu")
public class Student {
@Value("${msg1}")
private String stuName;
@Value("${msg2}")
private int stuHeight;
@Override
public String toString() {
return "Student{" +
"stuName='" + stuName + '\'' +
", stuHeight=" + stuHeight +
'}';
}
}
applicationContext.xml:
<context:property-placeholder location="classpath:message.properties"></context:property-placeholder>
3.其他注解
@Primary
含义:首选项,当类型冲突的情况下,此注解修饰的类被列为首选(备胎扶正)
修饰:类
注意:不能单独使用,必须与@Component....联合使用
@Qualifier(value="名称")
含义:按照名称装配
修饰:成员变量
注意:不能单独使用,必须与@Autowired联合使用
@Resource(name="名称")
含义:按照名称装配
修饰:成员变量
注意:单独使用
@Scope
含义:配置类的作用域
修饰:类
注意:不能单独使用,必须与@Component....联合使用
@Scope("prototype")
@Scope("singleton")
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
@Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)
@PostConstruct:初始化,修饰方法
@PreDestroy:销毁,修饰方法
3.0配置类语法:
spring中的新注解
@Configuration
作用:指定当前类是一个配置类
细节:当配置类作为AnnotationConfigApplicationContext对象创建的参数时,该注解可以不写。
@ComponentScan
作用:用于通过注解指定spring在创建容器时要扫描的包
属性:value:它和basePackages的作用是一样的,都是用于指定创建容器时要扫描的包。
替换:<context:component-scan base-package="com.apesource"></context:component-scan>
@PropertySource
作用:用于指定properties文件的位置
属性:value:指定文件的名称和路径。
配合@Value使用
替换:<context:property-placeholder location="classpath:message.properties"></context:property-placeholder>
@Bean
作用:用于把当前方法的返回值作为bean对象存入spring的容器中
属性:name:用于指定bean的id。当不写时,默认值是当前方法的名称
@Import
作用:用于导入其他的配置类
属性:value:用于指定其他配置类的字节码。
例子:@Import(SystemSpringConfig.class)
@Configuration
@ComponentScan(basePackages = "com.apesource")
@PropertySource(value = "classpath:message.properties")
//@Import(TestConfig.class)
public class ApplicationConfig {
// @Bean
// public IUserDao dao(){
// return new UserDaoImp();
// }
// @Bean
// public IUserService service(){
// return new IUserServiceImpl(dao());
// }
// @Bean
// public IUserController controller(){
// return new IUserControllerImpl(service());
// }
@Bean
public IUserDao dao(){
return new UserDaoImp();
}
@Bean
public IUserService service(/*@Autowired*/IUserDao dao){
return new IUserServiceImpl(dao);
}
@Bean
public IUserController controller(/*@Autowired*/IUserService service){
return new IUserControllerImpl(service);
}
}
import com.apesource.config.ApplicationConfig;
import com.apesource.config.TestConfig;
import com.apesource.controller.IUserController;
import com.apesource.dao.IUserDao;
import com.apesource.pojo.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.Date;
public class Test01 {
public static void main(String[] args) {
ApplicationContext context=new AnnotationConfigApplicationContext(ApplicationConfig.class);
// Student student=(Student)context.getBean("stu");
// System.out.println(student);
// IUserDao dao=(IUserDao)context.getBean("dao");
// IUserDao dao1=(IUserDao)context.getBean("dao1");
// Date time=(Date)context.getBean("time");
// System.out.println(dao);
// System.out.println(dao1);
// System.out.println(time);
//
IUserController controller=(IUserController)context.getBean("controller");
controller.save();
}
}