Spring学习记录2
生命周期
1 通过构造器创建bean实例 -创建
2 为bean的属性设置值和对其他bean方法的引用 -DI
3 调用bean的初始化方法(需要配置初始化的方法) -初始化
4 bean可以使用了 -使用
5当容器关闭时,调用bean的销毁方法(需要进行配置的销毁方法) -销毁
后置处理
1 通过构造器创建bean实例 -创建
2 为bean的属性设置值和对其他bean方法的引用 -DI
3把bean方法传递给bean的后置处理器的方法
4 调用bean的初始化方法(需要配置初始化的方法) -初始化
5把bean方法传递给bean的后置处理器的方法
6 bean可以使用了 -使用
7当容器关闭时,调用bean的销毁方法(需要进行配置的销毁方法) -销毁
详情看其他的解释
在xml中添加后置处理器之后,会为所有的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"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="orders" class="Furrain.bean.Orders" p:oName="手机" init-method="initMethod" destroy-method="destoryMethod"></bean>
<!---->
<bean id="mybeanpost" class="Furrain.bean.MyBeanPost"></bean>
</beans>
package Furrain.bean;
/**
* <Description>
*
* @author Furrain
* @version 1.0
* @ClassName Orders
* @createDate 2020/12/13 13:35
* @see Furrain.bean
*/
public class Orders {
public Orders() {
System.out.println("第一步,执行无参构造");
}
private String oName;
public void setoName(String oName) {
this.oName = oName;
System.out.println("第二步,进行setter");
}
//创建执行初始化方法
public void initMethod() {
System.out.println("第三部,执行初始化");
}
public void destoryMethod() {
System.out.println("第五步,销毁");
}
}
@Test
public void testBean6() {
ApplicationContext context = new ClassPathXmlApplicationContext("spring/bean6.xml");
Orders o = context.getBean("orders",Orders.class);
System.out.println("第四部,获取了实例");
((ClassPathXmlApplicationContext)context).close();
}
基于xml的自动装配
之前都是在xml中自己写property的值。算是手动装配
z自动装配,根据指定的装配规则(属性名称或者属性类型),Spring自动匹配属性进行注入
autowire byName
注入值的bean id 需要与类的对象名称一致
package Furrain.AutoWire;
/**
* <Description>
*
* @author Furrain
* @version 1.0
* @ClassName Emp
* @createDate 2020/12/13 14:36
* @see Furrain.AutoWire
*/
public class Emp {
private Dept eDept;
public void seteDept(Dept eDept) {
this.eDept = eDept;
}
@Override
public String toString() {
System.out.println(eDept);
return super.toString();
}
}
<?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 https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="emp" class="Furrain.AutoWire.Emp" autowire="byName">
<!-- 实现自动装配
auto-wire
byName
byType-->
</bean>
<bean id="eDept" class="Furrain.AutoWire.Dept" p:dName="xxxx"></bean>
</beans>
这里 代码中类对象的名称叫eDept bean中的id也要叫eDept,才能匹配上
autowire byType
与上面差不多,不过是对类型判断的,所以如果是byType id不是eDept也是可以的
基于xml的引用外部配置
当所有的属性值都写在mxl里的时候不是很方便,看起来多 又乱,所以可以写在外部的properties文件里,用的时候引入xml里
例子
例如使用数据库,写一个properties文件保存数据库的链接信息(这里用了阿里的德鲁伊库还有mysql的java连接器jar包)
prop.driverClass=com.mysql.jdbc.Driver
prop.url=jdbc:mysql://localhost:3306/xxxx
prop.userName=root
prop.password=xxxxxx
引入xml需要用到一个命名空间context
<?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"
<!--这里xxxxxxxxxxxxxxxxxxxxx-->
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!--这里xxxxxxxxxxxxxxxxxxxxx-->
<!--引入外部属性文件-->
<context:property-placeholder location="spring/jdbc.properties"></context:property-placeholder>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${prop.driverClass}"></property>
<property name="url" value="${prop.url}"></property>
<property name="username" value="${prop.userName}"></property>
<property name="password" value="${prop.password}"></property>
</bean>
</beans>
测试代码
@Test
public void testJdbc(){
ApplicationContext context = new ClassPathXmlApplicationContext("spring/bean8.xml");
DruidDataSource dsource = context.getBean("dataSource",DruidDataSource.class);
try {
Connection conn = dsource.getConnection();
String sql = "select * from user";
//6. 创建执行sql对象
Statement stmt = conn.createStatement();
//7. 创建一个ResultSet:结果集对象,封装查询结果
ResultSet rs = stmt.executeQuery(sql);
//8. 然后在用ResultSet里的方法 next():游标向下移动一行 判断是否有数据 有就是true,没有就是false
while (rs.next()) {
//rs.getString():传递的是数据库的字段
String username = rs.getString("email");
String password = rs.getString("password");
//9. 输入结果
System.out.println(username + " " + password);
}
} catch (SQLException e) {
e.printStackTrace();
}
}