Bean 默认是单例模式,即变量会跟静态变量似得,方法前加上@Scope("prototype")会变成多例模式
scope="singleton"
配置文件
<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">
<!-- 按照模块或者层次分类 -->
<import resource="beans.xml"/>
<!--
实现原理
1. 加载xml配置文件,解析文件
2. 读取bean节点的id和class属性值
3. 使用反射技术 Class.forName("类的路径").newInstance();得到对象 默认调用无参构造方法
4. 读取property节点中的属性,调用对象的setXXX()方法进行赋值
5. getBean("id")获取对象
-->
<!--
bean:代表的就是一个javabean对象
id:唯一标识这个实体对象
class:代表的就是实体对象的类
property:代表就是属性
name:当前对象属性名
value:当前对象属性值 普通字符串
ref:属性注入一个已经存在的对象
-->
bean必须设置为scope=singleton 才能执行destory-method中的销毁方法。
<bean id="springstu" class="com.yh.Student" scope="prototype" init-method="init" destroy-method="destory">
<property name="name" value="张三"/>
<property name="age" value="20"/>
</bean>
<!-- 定义一个手机对象 -->
<bean id="springphone" class="com.yh.Phone">
<property name="brand" value="华为"/>
<property name="price" value="3500"/>
</bean>
<bean id="springstu2" class="com.yh.Student">
<property name="name" value="李四"/>
<property name="age" value="10"/>
<property name="phone" ref="springphone"/>
</bean>
<!-- 静态工厂生成一个实例对象 -->
<bean id="staticstu" class="com.yh.MyStaticFactory" factory-method="getStu"/>
<!-- 实例工厂生成实例对象 -->
<!-- 配置实例工厂bean对象 -->
<bean id="mybeanfactory" class="com.yh.MyBeanFactory"/>
<!-- 指定实例工厂对象 生成实例对象stu -->
<bean id="mystu" factory-bean="mybeanfactory" factory-method="getBean"/>
<!-- 1.写一个类实现FactoryBean接口,重写getObject方法 2.配置标准工厂类 -->
<bean id="myStandBeanFactory" class="com.yh.MyStandBeanFactory"/>
</beans>
用对象作为参数
<bean id="springphone" class="com.yh.Phone">
<property name="brand">
<bean class = "com.yh.Phone.brand"/>
</property>
<property name="price" value="3500"/>
</bean>
变量注入
依赖注入
依赖注入配置文件
<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">
<!-- 使用setter方法 -->
<bean id="springstu" class="com.yh.Student" init-method="init" destroy-method="destory">
<property name="name" value="张三"/>
<property name="age" value="20"/>
</bean>
<bean id="springphone" class="com.yh.Phone">
<property name="brand" value="华为"/>
<property name="price" value="3500"/>
</bean>
<bean class="com.scope.MyBeanPost"/>
<!-- 使用有参构造方法注入参数值 -->
<bean id="ycStu" class="com.yh.Student">
<constructor-arg index="0" type="String" value="李四"/>
<constructor-arg index="1" type="int" value="10"/>
<constructor-arg index="2" type="com.yh.Phone" ref="springphone"/>
</bean>
<!-- 使用p命名空间 setter方法 -->
<bean id="pstu" class="com.yh.Student" p:name="王五" p:age="30" p:phone-ref="springphone"> </bean>
<!-- 集合注入值 -->
<bean id="user" class="com.scope.User">
<property name="names">
<set>
<value>赵六</value>
<value>tony</value>
</set>
</property>
<property name="phones">
<list>
<ref bean="springphone"/>
<ref bean="springphone"/>
</list>
</property>
<property name="cps">
<map>
<entry key="肖战" value="王一博"> </entry>
<entry key="胡歌" value="霍建华"/>
</map>
</property>
</bean>
</beans>
手动注入:setter方法注入
有参构造方法
p命名空间:导入约束(setter方法)
Spring属性注入有四种方式,
1.
set方法注入
<bean name="user" class="com.rr.bean.User">
<property name="age" value="20"></property>
</bean>
2.
构造函数注入
<bean name="user2" class="com.rr.bean.User">
<constructor-arg name="car" ref="car"></constructor-arg>
</bean>
3.
p名称空间注入
<bean name="user3" class="com.rr.bean.User" p:name="Jack" p:age="20" p:car-ref="car" ></bean>
4.
spel(Spring Expression Language)注入
配置文件中加入(这里取到了之前注入对象的值类型的值,不能取引用类型的值):
<bean name="user4" class="com.rr.bean.User" >
<property name="name" value="#{user.name}"></property>
<property name="age" value="#{user3.age}"></property>
<property name="car" ref="car"></property>
</bean>
集合注入值
set
list
map
bean三种配置方式
1.
默认构造方法
2.
静态工厂方法
3.
实例工厂方法--标准做法
name和id属性
name 可以重复,id不可以。
bean后处理器()
1. 写一个类实现BeanPostProcessor接口,重写before和after方法
2. 在配置文件中配置,不需要id
实例工厂
<bean id="mqFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<constructor-arg value="tcp://localhost:61616" />
</bean>
<bean id="mqConnection" factory-bean="mqFactory" factory-method="createConnection" init-method="start"/>
等同于
ActiveMQConnectionFactory connectionFactory =
new ActiveMQConnectionFactory("tcp://localhost:61616");
Connection connection = connectionFactory.createConnection();
connection.start();
1.
实例工厂
public class MyBeanFactory {
public Student getBean(){
Student s = new Student();
s.setName("赵六");
s.setAge(30);
return s;
}
}
<!-- 1. 配置实例工厂bean对象 -->
<bean id="mybeanfactory" class="com.yh.MyBeanFactory"/>
<!-- 2. 指定实例工厂对象 生成实例对象stu -->
<bean id="mystu" factory-bean="mybeanfactory" factory-method="getBean"/>
等同于 new MyBeanFactory.getBean();
2.
静态实例工厂
public class MyStaticFactory {
public static Student getStu(){
Student stu = new Student();
stu.setName("王五");
stu.setAge(20);
return stu;
}
}
<!-- 静态工厂生成一个实例对象 -->
<bean id="staticstu" class="com.yh.MyStaticFactory" factory-method="getStu"/>
等同于 MySaticFactory.getStu();
3.
标准实例工厂
<!-- 1.写一个类实现FactoryBean接口,重写getObject方法 -->
import org.springframework.beans.factory.FactoryBean;
public class MyStandBeanFactory implements FactoryBean<Student> {
public Student getObject() throws Exception {
Student s = new Student();
s.setName("jack");
s.setAge(20);
return s;
}
public Class<?> getObjectType() {
return null;
}
public boolean isSingleton() {
return false;
}
}
<bean id="myStandBeanFactory" class="com.yh.MyStandBeanFactory"/>
等同于 new MyStandBeanFactory(); 调用的是默认的无参构造方法
注解配置实例类
后面加上括号(“name”)
@Component
在类前加上这个注解标记这个类为bean
@Service @Repository @Controller
和上一个注解一样,只不过更精细,一眼能知道什么组件。 服务层 实体层 控制层
@Scope
@Autowired
自动依赖注入
加在属性前,这一个属性就会根据类型自动被赋值
@Autowired
@Qualifier("mysqldao")*/
上下等效,指定注入哪一个。
@Resource(name="mysqldao")
创建销毁回调函数
Bean类里
@PostConstruct
public void init(){
System.out.println("init...");
}
@PreDestroy
public void destory(){
System.out.println("destory");
}
Scope
singleton 单例模式,在每个Sping Ioc容器中一个bean定义对应一个对象实例
prototype 多例模式
request 单例模式,每次请求都会有各自的bean实例。 只在基于web的Spring ApplicationContext下有效
session 单例模式。 只在基于web的Spring ApplicationContext下有效
global session 单例模式,典型情况下,仅在使用portlet context 的时候有效。只在基于web的Spring ApplicationContext下有效
配置
1.
<bean id="springstu" class="com.yh.Student" scope="prototype">
2.
@Scope("prototype")
实例使用
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
UserServlet us = (UserServlet)ac.getBean("us");
简单测试
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:./com/spring/aop/annotation/applicationContext.xml")
public class Test1 {
@Resource(name="us")
private UserService us;
@Test
public void test1() {
/*ApplicationContext ac = new ClassPathXmlApplicationContext("./com/spring/aop/applicationContext.xml");
UserService us = (UserService)ac.getBean("userService");
us.add();*/
us.add();
}
}
评论区