Spring配置文件之Bean标签
Spring配置文件
1、Bean标签范围配置
1.1 当scope的取值为singleton时
Bean的实例化个数:1个
Bean的实例化时机:当Spring核心文件被加载时,实例化配置的Bean实例
Bean的生命周期:
对象创建:当应用加载,创建容器时,对象就被创建了
对象运行:只要容器在,对象一直活着
对象销毁:当应用卸载,销毁容器时,对象就被销毁了
1.2 当scope的取值为prototype时
Bean的实例化个数:多个
Bean的实例化时机:当调用getBean()方法时实例化Bean
Bean的生命周期:
对象创建:当使用对象时,创建新的对象实例
对象运行:只要对象在使用中,就一直活着
对象销毁:当对象长时间不用时,被Java的垃圾回收器回收了
1.3 相关实例
(1)创建一个接口
package com.itheima.dao;
public interface UserDao {
public void save();
}
(2)将接口进行实现
package com.itheima.dao.impl;
import com.itheima.dao.UserDao;
public class UserDaoImpl implements UserDao {
public UserDaoImpl() {
System.out.println("UserDaoImpl创建.....");
}
public void save(){
System.out.println("save running......");
}
}
(3)创建applicationContext.xml文件,scope参数就在里定义的
<?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="userDao" class="com.itheima.dao.impl.UserDaoImpl" scope="singleton"></bean>
</beans>
(4)创建一个主函数demo例子
package com.itheima.demo;
import com.itheima.dao.UserDao;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class UserDaoDemo {
public static void main(String[] args) {
ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
UserDao userDao = (UserDao) app.getBean("userDao");
userDao.save();
}
}
(5)进行junit测试
package com.itheima.test;
import com.itheima.dao.UserDao;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringTest {
@Test
//测试scope属性
/**当XML文件里的scope属性为singleton时,是单例的(默认值)
* 当scope属性为prototype时,是多例的
*两者创建时机是不一样的
* singleton是在加载配置文件时创建(当应用加载,创建容器时,对象就被创建了)
* prototype是在使用对象时,创建新的对象实例
*/
public void test1(){
ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
UserDao userDao1 = (UserDao) app.getBean("userDao");
UserDao userDao2 = (UserDao) app.getBean("userDao");
System.out.println(userDao1);
System.out.println(userDao2);
}
}
2、Bean生命周期配置
2.1 相关参数
init-method: 指定类中的初始化方法名称
destroy-method: 指定类中销毁方法名称
2.2 相关代码
和上面代码一样,只有细节方面改变
(1) 在applicationContext.xml文件里面多加了init-method参数和destroy-method参数
<?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="userDao" class="com.itheima.dao.impl.UserDaoImpl"
scope="singleton"
init-method="init"
destroy-method="destory">
</bean>
</beans>
(2) 在实现接口的类里加了init()和destory()方法
package com.itheima.dao.impl;
import com.itheima.dao.UserDao;
public class UserDaoImpl implements UserDao {
public UserDaoImpl() {
System.out.println("UserDaoImpl创建.....");
}
public void init(){
System.out.println("初始化方法...");
}
public void destory(){
System.out.println("销毁方法...");
}
public void save(){
System.out.println("save running......");
}
}
(3) 在测试类里修改了一些方法
package com.itheima.test;
import com.itheima.dao.UserDao;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringTest {
@Test
public void test1(){
ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
UserDao userDao1 = (UserDao) app.getBean("userDao");
System.out.println(userDao1);
app.close();
}
}
3、Bean实例化三种方式
3.1 相关方式
无参构造方法实例化
工厂静态方法实例化
工厂实例方法实例化
3.2 工厂静态方法实例化
代码和最初的也差不多
(1)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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--<bean id="userDao" class="com.itheima.dao.impl.UserDaoImpl" scope="singleton" init-method="init" destroy-method="destory"></bean>-->
<bean id="userDao" class="com.itheima.factory.StaticFactory" factory-method="getUserDao"></bean>
</beans>
(2)增加了一个静态化工厂
package com.itheima.factory;
import com.itheima.dao.UserDao;
import com.itheima.dao.impl.UserDaoImpl;
public class StaticFactory {
public static UserDao getUserDao(){
return new UserDaoImpl();
}
}
3.3 工厂实例方法实例化
代码和最初的也差不多
(1)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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--<bean id="userDao" class="com.itheima.dao.impl.UserDaoImpl" scope="singleton" init-method="init" destroy-method="destory"></bean>-->
<!--<bean id="userDao" class="com.itheima.factory.StaticFactory" factory-method="getUserDao"></bean>-->
<bean id="factory" class="com.itheima.factory.DynamicFactory"></bean>
<bean id="userDao" factory-bean="factory" factory-method="getUserDao"/>
</beans>
(2)增加了一个实例化工厂
package com.itheima.factory;
import com.itheima.dao.UserDao;
import com.itheima.dao.impl.UserDaoImpl;
public class DynamicFactory {
public UserDao getUserDao(){
return new UserDaoImpl();
}
}
3、Bean的依赖注入分析
因为UserService和UserDao都在Spring容器中,而最终程序直接使用的是UserService,所以可以在Spring容器中,
将UserDao设置到UserService内部。
getBean(“userService”)
获得UserService实例,内部已经存在UserDao实例了,直接调用UserDao的save()方法即可
getBean(“userDao”)
相当于是在Spring容器外将Dao组装到Service内部完成的操作
3、Bean的依赖注入概念
依赖注入:它是Spring框架核心IOC的具体体现
在编写程序时,通过控制反转,把对象的创建交给了Spring,但是代码中不可能出现没有依赖的情况。
IOC解耦只是降低他们的依赖关系,但不会消除。例如:业务层仍会调用持久层的方法。
那这种业务层和持久层的依赖关系,在使用Spring之后,就让Spring来维护了。
简单来说,就是坐等框架把持久层对象传入业务层,而不用我们自己去获取。
3.1 Bean的依赖注入方式
3.1.1构造方法
(1)修改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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--<bean id="userDao" class="com.itheima.dao.impl.UserDaoImpl" scope="singleton" init-method="init" destroy-method="destory"></bean>-->
<!--<bean id="userDao" class="com.itheima.factory.StaticFactory" factory-method="getUserDao"></bean>-->
<!--工厂
<bean id="factory" class="com.itheima.factory.DynamicFactory"></bean>
<bean id="userDao" factory-bean="factory" factory-method="getUserDao"/>
-->
<bean id="userDao" class="com.itheima.dao.impl.UserDaoImpl"></bean>
<!--
<bean id="userService" class="com.itheima.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao"></property>
</bean>-->
<!-- P命名空间
<bean id="userService" class="com.itheima.service.impl.UserServiceImpl" p:userDao-ref="userDao"/>
-->
<bean id="userService" class="com.itheima.service.impl.UserServiceImpl">
<constructor-arg name="userDao" ref="userDao"></constructor-arg>
</bean>
</beans>
3.1.2 set方法
代码和最初的也差不多
(1)修改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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--<bean id="userDao" class="com.itheima.dao.impl.UserDaoImpl" scope="singleton" init-method="init" destroy-method="destory"></bean>-->
<!--<bean id="userDao" class="com.itheima.factory.StaticFactory" factory-method="getUserDao"></bean>-->
<!--
<bean id="factory" class="com.itheima.factory.DynamicFactory"></bean>
<bean id="userDao" factory-bean="factory" factory-method="getUserDao"/>
-->
<bean id="userDao" class="com.itheima.dao.impl.UserDaoImpl"></bean>
<bean id="userService" class="com.itheima.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao"></property>
</bean>
</beans>
package com.itheima.service.impl;
import com.itheima.dao.UserDao;
import com.itheima.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class UserServiceImpl implements UserService {
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public void save() {
userDao.save();
}
}
3.1.3 set方法2
P命名空间注入本质也是set方法注入,但比较上述的set方法注入更加方便,主要体现在配置文件中,
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--<bean id="userDao" class="com.itheima.dao.impl.UserDaoImpl" scope="singleton" init-method="init" destroy-method="destory"></bean>-->
<!--<bean id="userDao" class="com.itheima.factory.StaticFactory" factory-method="getUserDao"></bean>-->
<!--
<bean id="factory" class="com.itheima.factory.DynamicFactory"></bean>
<bean id="userDao" factory-bean="factory" factory-method="getUserDao"/>
-->
<bean id="userDao" class="com.itheima.dao.impl.UserDaoImpl"></bean>
<!--
<bean id="userService" class="com.itheima.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao"></property>
</bean>-->
<bean id="userService" class="com.itheima.service.impl.UserServiceImpl" p:userDao-ref="userDao"/>
</beans>
4、Bean的依赖注入的数据类型
除了对象的引用可以注入,普通数据类型,集合等都可以在容器中进行注入。
4.1 注入数据的三种数据类型
(1)普通数据类型
代码和最初的也差不多
修改的UserDaoImpl代码
package com.itheima.dao.impl;
import com.itheima.dao.UserDao;
public class UserDaoImpl implements UserDao {
private String username;
private int age;
public void setUsername(String username) {
this.username = username;
}
public void setAge(int age) {
this.age = age;
}
// public UserDaoImpl() {
// System.out.println("UserDaoImpl创建.....");
// }
//
// public void init(){
// System.out.println("初始化方法...");
// }
//
// public void destory(){
// System.out.println("销毁方法...");
// }
public void save(){
System.out.println(username + "======" + age);
System.out.println("save running......");
}
}
修改的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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--<bean id="userDao" class="com.itheima.dao.impl.UserDaoImpl" scope="singleton" init-method="init" destroy-method="destory"></bean>-->
<!--<bean id="userDao" class="com.itheima.factory.StaticFactory" factory-method="getUserDao"></bean>-->
<!--工厂
<bean id="factory" class="com.itheima.factory.DynamicFactory"></bean>
<bean id="userDao" factory-bean="factory" factory-method="getUserDao"/>
-->
<bean id="userDao" class="com.itheima.dao.impl.UserDaoImpl">
<property name="username" value="zhangshan"/>
<property name="age" value="18"/>
</bean>
<!--
<bean id="userService" class="com.itheima.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao"></property>
</bean>-->
<!-- P命名空间
<bean id="userService" class="com.itheima.service.impl.UserServiceImpl" p:userDao-ref="userDao"/>
-->
<bean id="userService" class="com.itheima.service.impl.UserServiceImpl">
<constructor-arg name="userDao" ref="userDao"></constructor-arg>
</bean>
</beans>
(2)引用数据类型
(3)集合数据类型
修改的UserDaoImpl代码
package com.itheima.dao.impl;
import com.itheima.dao.UserDao;
import com.itheima.domain.User;
import java.util.List;
import java.util.Map;
import java.util.Properties;
public class UserDaoImpl implements UserDao {
private List<String> strList;
private Map<String, User> userMap;
private Properties properties;
public List<String> getStrList() {
return strList;
}
public void setStrList(List<String> strList) {
this.strList = strList;
}
public Map<String, User> getUserMap() {
return userMap;
}
public void setUserMap(Map<String, User> userMap) {
this.userMap = userMap;
}
public Properties getProperties() {
return properties;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
private String username;
private int age;
public void setUsername(String username) {
this.username = username;
}
public void setAge(int age) {
this.age = age;
}
// public UserDaoImpl() {
// System.out.println("UserDaoImpl创建.....");
// }
//
// public void init(){
// System.out.println("初始化方法...");
// }
//
// public void destory(){
// System.out.println("销毁方法...");
// }
public void save(){
//System.out.println(username + "======" + age);
System.out.println(strList);
System.out.println(userMap);
System.out.println(properties);
System.out.println("save running......");
}
}
修改的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:p="http://www.springframework.org/schema/p"
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">
<!--<bean id="userDao" class="com.itheima.dao.impl.UserDaoImpl"
scope="singleton" init-method="init" destroy-method="destory">
</bean>-->
<!--<bean id="userDao" class="com.itheima.factory.StaticFactory"
factory-method="getUserDao">
</bean>-->
<!--工厂模式-->
<!--
<bean id="factory" class="com.itheima.factory.DynamicFactory"></bean>
<bean id="userDao" factory-bean="factory" factory-method="getUserDao"/>
-->
<!--普通数据类型注入-->
<!--
<bean id="userDao" class="com.itheima.dao.impl.UserDaoImpl">
<property name="username" value="zhangshan"/>
<property name="age" value="18"/>
</bean>
-->
<!--集合数据类型注入-->
<bean id="userDao" class="com.itheima.dao.impl.UserDaoImpl">
<property name="strList">
<list>
<value>aaa</value>
<value>bbb</value>
<value>ccc</value>
</list>
</property>
<property name="userMap">
<map>
<entry key="u1" value-ref="user1"></entry>
<entry key="u2" value-ref="user2"></entry>
</map>
</property>
<property name="properties">
<props>
<prop key="p1">pppl</prop>
<prop key="p2">ppp2</prop>
<prop key="p3">ppp3</prop>
</props>
</property>
</bean>
<bean id="user1" class="com.itheima.domain.User">
<property name="name" value="tom"/>
<property name="adder" value="beijing"/>
</bean>
<bean id="user2" class="com.itheima.domain.User">
<property name="name" value="lucy"/>
<property name="adder" value="tianjing"/>
</bean>
<!--
<bean id="userService" class="com.itheima.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao"></property>
</bean>-->
<!-- P命名空间-->
<!--
<bean id="userService" class="com.itheima.service.impl.UserServiceImpl" p:userDao-ref="userDao"/>
-->
<bean id="userService" class="com.itheima.service.impl.UserServiceImpl">
<constructor-arg name="userDao" ref="userDao"></constructor-arg>
</bean>
</beans>
结果:
5、引入其他配置文件(分模块开发)
实际开发中,Spring的配置内容非常多,这就导致Spring配置很繁杂且体积很大,所以可以将部分配置拆解到其他配置文件中,而在Spring主配置文件通过import标签进行加载。
6、Spring的重点配置
<bean>标签
id属性: 在容器中Bean实例的唯一标识,不允许重复
class属性:要实例化的Bean的全限名
scope属性:Bean的作用范围,常用是Singleton(默认)和prototype
<property>标签:属性注入
name属性:属性名称
value属性:注入的普通属性值
ref属性:注入的对象引用值
<list>标签
<map>标签
<properties>标签
<constructor-arg>标签
<import>标签:导入其他的spring的分文件
7、Spring相关API
7.1 ApplicationContext的实现类
(1)ClassPathXmlApplicationContext
从类的根路径下加载配置文件(推荐)
(2)FileSystemXmlApplicationContext
从磁盘路径上加载配置文件,配置文件可以在磁盘的任意位置
(3)AnnotationConfigApplicationContext
当使用注解配置容器对象时,需要使用此类来创建spring容器,它用来读取注解
(4)getBean()方法使用
当参数的数据类型是字符串时,表示根据Bean的id从容器中获得Bean实例,返回是Object,需要强转。
当参数的数据类型是Class类型时,表示根据类型从容器中匹配Bean实例,当容器中相同类型的Bean有多个时,则此方法会报错。
Spring的重点API
ApplicationContext app = new ClasspathXmlApplicationContext("xml文件")
app.getBean("id")
app.getBean(Class)