Bean的配置
如果把Spring看做一个大型工厂,则Spring容器中的Bean就是该工厂的产品。要想使用这个工厂生产和管理Bean,就需要在配置文件中告诉它需要哪些Bean,以及需要使用何种方式将这些Bean装配到一起
在实际开发中,最常使用的是XML文件格式的配置方式,这种配置方式是通过XML文件来注册并管理Bean之间的依赖关系。
在配置文件中,通常一个普通的Bean只需要定义id(或name)和class 两个属性即可,定义Bean的方式如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="bean1" class="com.itheima.Bean1" />
<bean name="bean2" class="com.itheima.Bean2" />
</beans>
Bean的实例化
在面向对象的程序中,想要使用某个对象,就需要先实例化这个对象。
同样,在Spring中,要想使用容器中的Bean,也需要实例化Bean。
实例化Bean有三种方式,分别为构造器实例化、静态工厂方式实例化和实例工厂方式实例化
(其中最常用的是构造器实例化)。
构造器实例化
1.创建Web项目,导入相关Jar包;
2.创建名为Bean1的Java类;
3.创建Spring配置文件beans1.xml,并配置Bean1实体类Bean
4.创建测试类,测试程序
public class InstanceTest1 {
public static void main(String[] args) {
String xmlPath = "com/itheima/instance/constructor/beans1.xml";
ApplicationContext applicationContext =
new ClassPathXmlApplicationContext(xmlPath);
Bean1 bean = (Bean1) applicationContext.getBean("bean1");
System.out.println(bean);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">
<bean id="bean1" class="com.itheima.instance.constructor.Bean1" />
</beans>
Bean的作用域
Spring 4.3中为Bean的实例定义了7种作用域
singleton和prototype是最常用的两种作用域
singleton作用域
singleton是Spring容器默认的作用域,当Bean的作用域为singleton时,Spring容器就只会存在一个共享的Bean实例。singleton作用域对于无会话状态的Bean(如Dao 组件、Service组件)来说,是最理想的选择。
在Spring配置文件中,可以使用元素的scope属性,将Bean的作用域定义成singleton。
<bean id="scope" class="com.itheima.scope.Scope" scope="singleton"/>
prototype作用域
对需要保持会话状态的Bean(如Struts 2的Action类)应该使用prototype作用域。在使用prototype作用域时,Spring容器会为每个对该Bean的请求都创建一个新的实例。
在Spring配置文件中,同样使用<bean>元素的scope属性,将Bean的作用域定义成prototype 。
<bean id="scope" class="com.itheima.scope.Scope" scope=" prototype "/>
Bean的生命周期
Spring中Bean的生命周期的意义:可以利用Bean在其存活期间的特定时刻完成一些相关操作
singleton作用域:
Spring容器可以管理singleton作用域的Bean的生命周期,在此作用域下,Spring能够精确的知道该Bean何时被创建,何时初始化完成,以及何时被销毁。
prototype作用域:
prototype作用域的Bean,Spring只负责创建,当容器创建了Bean实例后,Bean的实例就交给客户端代码来管理,Spring容器将不再跟踪其生命周期。
Bean的装配方式
Bean的装配可以理解为依赖关系注入,Bean的装配方式即Bean依赖注入的方式
Spring容器支持多种形式的Bean的装配方式,
如基于XML的装配、
基于注解(Annotation)的装配
和自动装配(其中最常用的是基于注解的装配)
基于注解(Annotation)的装配
Dao层的类
package com.itheima.annotation;
public interface UserDao {
public void save();
}
package com.itheima.annotation;
import org.springframework.stereotype.Repository;
@Repository("userDao")
public class UserDaoImpl implements UserDao {
public void save() {
System.out.println("userdao...save...");
}
}
Service层
package com.itheima.annotation;
public interface UserService {
public void save();
}
package com.itheima.annotation;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
@Service("userService")
public class UserServiceImpl implements UserService {
@Resource(name= "userDao")
private UserDao userDao;
@Override
public void save() {
//调用userDao的save()方法
this.userDao.save(); System.out.println("userservice...save...");
}
}
Controller层
package com.itheima.annotation;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
@Controller("userController")
public class UserController {
@Resource(name="userService")
private UserService userService;
public void setUserService(UserService userService) {
this.userService = userService;
}
public void save() {
this.userService.save();
System.out.println("userController...save...");
}
}
配置文件
<?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-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!-- 使用context命名空间,通知Spring扫描指定包下所有的Bean类,进行注解解析 -->
<context:component-scan base-package="com.itheima.annotation"/>
<!-- 使用context命名空间,在配置文件中开启相应的注解处理器 -->
<context:annotation-config />
<bean id ="userDao" class="com.itheima.annotation.UserDaoImpl"/>
<bean id="userService" class="com.itheima.annotation.UserServiceImpl"/>
<bean id="userController" class="com.itheima.annotation.UserController"/>
</beans>
运行效果