Spring学习——IOC由xml实现

Spring框架的基础模块,包含IOC(注入)和AOP(面向切面)。

基本配置使用

基础配置

src文件夹下,创建xml文件,这个文件放置Spring的配置。一般使用beans.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-4.1.xsd">

</beans>

创建需要注入的类

public class HelloIoc {

    public void showMsg() {
        System.out.println("测试成功");
    }

}

在beans.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-4.1.xsd">

    <!-- 
        id:获取该注入类时,使用的字段
        class:该注入类的全部路径
     -->    
    <bean id="helloIoc" class="model.HelloIoc"></bean>

</beans>

创建测试类及测试方法

public class TestIoc {

    /**
     * 注入类,统一使用BeanFactory来获取。
     * BeanFactory的创建,则是通过以下方式
     */
    private BeanFactory factory = new ClassPathXmlApplicationContext("beans.xml");

    @Test
    public void testIocForXml() {
        HelloIoc helloIoc = factory.getBean("helloIoc",HelloIoc.class);
        helloIoc.showMsg();
    }

}

测试结果

测试成功

单例多例转换

Java中涉及到单例模式与多例模式,注入也会在这两种模式中取舍。

默认模式

首先,我们测试一下默认的模式。创建两个对象,对比两个对象是否相同。

public class TestIoc {

    /**
     * 注入类,统一使用BeanFactory来获取。
     * BeanFactory的创建,则是通过以下方式
     */
    private BeanFactory factory = new ClassPathXmlApplicationContext("beans.xml");

    @Test
    public void testIocForXml() {
        HelloIoc helloIoc = factory.getBean("helloIoc",HelloIoc.class);
        //获取第二个对象
        HelloIoc helloIoc2 = factory.getBean("helloIoc",HelloIoc.class);
        helloIoc.showMsg();
        //对比两个对象
        System.out.println(helloIoc==helloIoc2);
    }

}

运行结果

测试成功
true

由此可见,默认模式下,获取的对象是单例。至于单例与多例的区别,这里不做赘述,有需要的小伙伴可以搜一搜。

设置多例/单例

在beans.xml配置文件中,bean标签下有个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-4.1.xsd">

    <!-- 
        id:获取该注入类时,使用的字段
        class:该注入类的全部路径
     -->
     <!-- 
        prototype:多例模式
        singleton:单例模式(默认)
     -->
    <bean id="helloIoc" class="model.HelloIoc" scope="prototype"></bean>

</beans>

运行结果

测试成功
false

变量注入

在一个类中,我们一般会使用到许多变量,那么我们学习一下这些变量的注入。

创建几个功能类

User类

public class User {

    private String name;

    private int id ;

    public User(int id,String name) {
        this.id = id;
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

}

UserDao类

public class UserDao {

    public void add(User user) {
        System.out.println("add " + user.getName());
    }

    public void delete(int id) {
        System.out.println("delete " + id);
    }

}

UserService类

public class UserService {

    private UserDao dao;

    public void add(User user) {
        dao.add(user);
    }

    public void delete(int id) {
        dao.delete(id);
    }

    public UserDao getDao() {
        return dao;
    }

    public void setDao(UserDao dao) {
        this.dao = dao;
    }

}

因为User类存在变量,我们先不对User注入。对UserDao和UserService的注入如下:

<?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.1.xsd">

    <!-- 
        id:获取该注入类时,使用的字段
        class:该注入类的全部路径
     -->
     <!-- 
        prototype:多例模式
        singleton:单例模式(默认)
     -->
    <bean id="helloIoc" class="model.HelloIoc" scope="prototype"></bean>

    <bean id="userDao" class="dao.UserDao"></bean>

    <bean id="userService" class="service.UserService">
        <!-- 
            name中的值会在userService对象中调用setXX方法来注入
            诸如:name="dao" ,在具体注入时,setDao(UserDao userDao) 完成注入
            ref="userDao" 表示配置文件中,bean中创建的id
        -->
        <property name="dao" ref="userDao"/>
    </bean>

</beans>

测试

写测试方法
    @Test
    public void testIoc2() {
        UserService service = factory.getBean("userService",UserService.class);
        User user = new User(1, "测试对象");
        service.setUser(user);
        service.add();
        service.delete(2);
    }

运行结果

add 测试对象
delete 2

测试结果出来,再叙述一下:当要注入的类中存在变量时(目前变量依然是注入类),我们可以使用property标签来实现注入:

    <bean id="userService" class="service.UserService">
        <!-- 
            name中的值会在userService对象中调用setXX方法来注入
            诸如:name="dao" ,在具体注入时,setDao(UserDao userDao) 完成注入
            ref="userDao" 表示配置文件中,bean中创建的id
        -->
        <property name="dao" ref="userDao"/>
    </bean>

实现非注入类的变量注入

User类中,String和int型变量都不是注入类,那么我们不能使用ref的方式,一般设置固定值时,都会使用value:

    <bean id="user" class="model.User" scope="prototype">
        <property name="name" value="注入名"></property>
        <property name="id" value="11"></property>
    </bean>

在UserService的bean配置中添加配置:

    <bean id="userService" class="service.UserService">
        <property name="dao" ref="userDao"/>
        <property name="user" ref="user"></property>
    </bean>

测试

因为这里已经注入,所以讲测试方法中User的创建与赋值去掉:

    @Test
    public void testIoc2() {
        UserService service = factory.getBean("userService",UserService.class);
//      User user = new User(1, "测试对象");
//      service.setUser(user);
        service.add();
        service.delete(2);
    }

运行一下测试方法,哇,出错了。

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userService' defined in class path resource [beans.xml]: Cannot resolve reference to bean 'user' while setting bean property 'user'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'user' defined in class path resource [beans.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [model.User]: No default constructor found; nested exception is java.lang.NoSuchMethodException: model.User.<init>()

这么一大堆错误信息,还只是第一行,其实只在说一件事:没有发现默认构造函数

即:注入类必须有一个无参构造函数

我们在User类中添加无参构造,再运行:

add 注入名
delete 2

自动注入

每个变量都需要配置bean标签,实在是麻烦透顶,那么你一定动小心思:如果能够自动注入就好了。
是了,Spring开发团队也想到了:autowire标签

再次修改UserService的bean配置:

    <bean id="userService" class="service.UserService" autowire="byType">
        <!-- 
        <property name="dao" ref="userDao"/>
        <property name="user" ref="user"/> 
        -->
    </bean>

测试结果

add 注入名
delete 2

如果这时候使用 byName,则会报错。因为在UserService中,UserDao变量的名称是dao,找不到相匹配的id,这里不再做修改。
自动注入并不是很推荐,如果有兴趣可以看看这个挺全的自动注入,这里我贴上各种模式的介绍:

模式作用
no这是默认设置,意味着它没有使用自动装配模式。你应该显示的使用bean引用来连接
byName由属性名自动装配。Spring 容器看到bean采用了自动装配byName模式(autowire=”byName”),然后根据它的属性在Spring 容器中寻找与属性名相同bean进行关联
byType由属性的数据类型自动装配。Spring容器看到bean采用了自动装配的byType模式(autowire=”byType”),然后根据属性类型在Spring容器中寻找与属性类型相同bean进行关联。如果存在不止一个这样的bean,将抛出异常。
constructor类似于 byType,但该类型适用于构造函数参数类型。如果在容器中没有一个构造函数参数类型的 bean,则一个致命错误将会发生
autodetectSpring首先尝试通过 constructor 使用自动装配来连接,如果它不执行,Spring 尝试通过 byType 来自动装配

List注入

最后说一种非常不常用的注入

    <property name="list">
            <!-- 同样可以注入列表,也不常用 -->
            <list>
                <value>159</value>
                <value>qqq</value>
                <value>www</value>  
                <value>eeee</value>
            </list>
        </property>

最后

正在学习中,如果有什么错误,请留言告诉我,会第一时间回复并修改。
即时更新地址

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值