Spring学习记录2

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();
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
该资源内项目源码是个人的课程设计、毕业设计,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分,放心下载使用! ## 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。 该资源内项目源码是个人的课程设计,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分,放心下载使用! ## 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值