• Spring注入(IOC):


    简单来说就是减少层与层之间的耦合关系,本来在service调用dao要new,有了这个就可以通过注入的方式,相当与把所有的new操作都变成了在配置文件中配置,有改动时直接改配置就行了不用一个个java文件去改。

    1.搭建web项目,导入spring核心包。copy到web目录lib即可。

    image

    项目的构成:

    image

    1.首先是创建一个bean。省略setter、getter

    public class UserBean{
        private String userName;
        private int age;
        private Date birthday;
        private double salary;
    }

    建立spring的配置文件application.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"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:tx="http://www.springframework.org/schema/tx"
        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
             http://www.springframework.org/schema/aop
             http://www.springframework.org/schema/aop/spring-aop.xsd
             http://www.springframework.org/schema/tx
              http://www.springframework.org/schema/tx/spring-tx.xsd">
              
        <import resource="application2.xml"/>      
        <bean id="UserBean" class="pojo.UserBean" scope="singleton" lazy-init="true">
            <!--     singleton – 单例模式 ,即整个应用只有一个实例 。适合于服务 bean 和Dao Bean。单例模式的对象实例在容器启动时候就会创建。
                        Prototype – 原型模式,即每次请求都创建一个对象。适合于action bean。原型模式的对象在请求对象时候创建 
                        lazy-init - 懒加载,默认=false,启动就创建(不懒),true就是懒加载
                -->
            <property name="userName" value="zs"></property>
            <property name="age" value="18"></property>
            <property name="birthday">
                <bean class="java.util.Date"></bean>
            </property>
            <property name="salary" value="9000.33"></property>
        </bean>
        
    </beans>

    单元测试,通过配置文件获得创建的对象。两种方式获得。都能打印出结果。

    public class UserBeanTest {
    
        @Test
        public void test() {
            //启动spring 容器,指定配置文件。
            ApplicationContext ctx=new ClassPathXmlApplicationContext("application.xml");
            //通过容器获取UserBean对象
            UserBean bean=(UserBean) ctx.getBean("UserBean");
            System.out.println(bean.toString());
        }
        @Test
        public void testByFactory(){
            //通过工厂类得到bean对象
            Resource resource= new ClassPathResource("application.xml");
            BeanFactory factory=new XmlBeanFactory(resource);
            //通过容器获取UserBean对象
            UserBean bean=(UserBean) factory.getBean("UserBean");
            System.out.println(bean.toString());
        }
    
    
    }

    结果:

    UserBean [userName=zs, age=18, birthday=Wed Sep 13 16:37:20 CST 2017, salary=9000.33]

    2.对象的注入。

    新建dao和impl

    public interface UserDao {
        public abstract void print();
    
    }
    
    
    ----------------------------------------------------------------------------------------
    public class UserDaoImpl implements UserDao {
        @Override
        public void print() {
            System.out.println("dao打印:");
        }
    
        
    }

    新建service和impl

    public interface UserService {
        public abstract void print();
    
    }
    
    
    -------------------------------------------------------------------------------------------
    public class UserServiceImpl implements UserService {
        //要注入它
        UserDao dao;
        @Override
        public void print() {
            dao.print();
        }
        public UserDao getDao() {
            return dao;
        }
        public void setDao(UserDao dao) {
            this.dao = dao;
        }
        
    
    }

    建立配置文件2号

    <?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"
        xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
        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
             http://www.springframework.org/schema/aop
             http://www.springframework.org/schema/aop/spring-aop.xsd
             http://www.springframework.org/schema/tx
              http://www.springframework.org/schema/tx/spring-tx.xsd">
    
        <!-- set注入:对应在服务层调用dao给它setter、getter -->
        <bean id="dao" class="dao.impl.UserDaoImpl"></bean>
        <bean id="service" class="service.impl.UserServiceImpl">
            <property name="dao" ref="dao"></property>
        </bean>
        
        <!-- 内部bean方式,也要setter、getter -->
        <bean id="service2" class="service.impl.UserServiceImpl">
            <property name="dao">
                <bean class="dao.impl.UserDaoImpl"></bean>
            </property>
        </bean>    
        
        <!--P:命名空间注入属性值 也要setter、getter,跟set注入一样的,就是减少了property的数量。p:dao-ref="dao" 中的'dao'换为对应的注入对象即可-->
        <bean id="service3" class="service.impl.UserServiceImpl" p:dao-ref="dao"></bean>
        
        <!--自动注入(autowire)  -->
        <bean id="service4" class="service.impl.UserServiceImpl" autowire="byName"></bean>
        <!--Aotowire=byType:根据名称属性类型自动进行注入。  -->
        <bean id="service5" class="service.impl.UserServiceImpl" autowire="byType"></bean>
    
    </beans>

    在原来的配置文件中加上import把两个连接起来。

    <import resource="application2.xml"/>

    单元测试。

    public class 各种注入方式 {
    
        @Test
        public void test() { //set注入
            /** application.xml 要import */
            ApplicationContext applicationContext=new ClassPathXmlApplicationContext("application.xml");
            /** 合并的写法 */
    //        ApplicationContext applicationContext=new ClassPathXmlApplicationContext(new String[]{"application.xml","application2.xml"}); 
            UserService service= (UserService) applicationContext.getBean("service");
            service.print();
        }
        @Test
        public void test2() { //内部bean
            ApplicationContext applicationContext=new ClassPathXmlApplicationContext("application.xml");
            UserService service= (UserService) applicationContext.getBean("service2");
            service.print();
        }
        @Test
        public void test3() { //命名空间注入
            ApplicationContext applicationContext=new ClassPathXmlApplicationContext("application.xml");
            UserService service= (UserService) applicationContext.getBean("service3");
            service.print();
        }
        @Test
        public void test4() { //按照名字自动注入
            ApplicationContext applicationContext=new ClassPathXmlApplicationContext("application.xml");
            UserService service= (UserService) applicationContext.getBean("service4");
            service.print();
        }
        @Test
        public void test5() { //按照类型自动注入
            ApplicationContext applicationContext=new ClassPathXmlApplicationContext("application.xml");
            UserService service= (UserService) applicationContext.getBean("service5");
            service.print();
        }
        @Test
        public void test6() { //获取bean的另一个写法,会自动在配置文件中找对应的class类型匹配,但是配置文件里面自能有一个对应的类,多了也错
            ApplicationContext applicationContext=new ClassPathXmlApplicationContext("application.xml");
            UserService service= (UserService) applicationContext.getBean(UserService.class);
            service.print();
        }
    
    }

    test6这种获取bean的方式要只有一个才可以,很多个bean都是一样的,仅仅是id不同会报错的。

    3.注解方式。

  • 相关阅读:
    Codeforces Round #610 (Div. 2)C(贪心,思维)
    Educational Codeforces Round 80 (Rated for Div. 2)C(DP)
    BZOJ2190 仪仗队
    BZOJ3613 南园满地堆轻絮
    BZOJ1084 最大子矩阵
    BZOJ1036 树的统计Count
    BZOJ1452 Count
    BZOJ2242 计算器
    BZOJ2705 Longge的问题
    BZOJ1509 逃学的小孩
  • 原文地址:https://www.cnblogs.com/lq625424841/p/7529283.html
Copyright © 2020-2023  润新知