• Spring_IOC


    我们都知道,如果要在不同的类中使用同一个对象一般我们我们都需要在每一个类中都去new一个新的对象,也有的人会为这个对象写一个工具类,无论哪种方法都需要我们自己去创建,不但繁琐,而且相当耗损资源,所以才有了Spring使用的必要性,也就是说,Spring的众多功能中为我们创建对象就是其中之一。话不多说直接上代码:

    1 /*
    2 *dao层接口
    3 */
    4 public interface AccountDao {
    5     void say();
    6 }
    1 /*
    2 * dao层实现类
    3 */
    4 public class AccountDaoImpl implements AccountDao {
    5     @Override
    6     public void say() {
    7         System.out.println("我是dao层>>>>>>>>>>>>>");
    8     }
    9 }
    /**
     * service层接口
     */
    public interface AccountService {
        void say();
    }
     1 /**
     2  * service实现类
     3  */
     4 public class AccountServiceImpl implements AccountService {
     5 
     6     private AccountDao dao;//创建dao层成员,并写出它的set,get方法
     7 
     8     @Override
     9     public void say() {
           dao.say();//直接调用dao层的方法
    10 System.out.println("我是service层>>>>>>>>>>>>>"); 11 } 12 13 //get方法 14 public AccountDao getDao() { 15 return dao; 16 } 17 //set方法 18 public void setDao(AccountDao dao) { 19 this.dao = dao; 20 } 21 }

    第一种纯XML方式

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
     5     <!--创建dao层的bean,其中class属性指向的dao接口的实现类,只能时实现类,不能是接口-->
     6     <bean id="dao" class="com.lhf.dao.impl.AccountDaoImpl"/>
     7     <!--创建service层的bean-->
     8     <bean id="service" class="com.lhf.service.impl.AccountServiceImpl">
     9         <!--向service层注入dao的bean,可以把它之间看成是在service层创建一个dao的对象,
    10         其中的name属性是service层的成员变量,如果成员是基本类型,可以用value属性直接为之赋值-->
    11         <property name="dao" ref="dao"/>
    12     </bean>
    13 </beans>

     创建测试类

     1 public class App 
     2 {
     3     public static void main( String[] args )
     4     {
     5         //spring读取xml配置文件
     6         ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
     7         //从spring容器中获取service层的对象
     8         AccountService bean = context.getBean(AccountService.class);
     9         //调用service层的方法
    10         bean.say();
    11     }
    12 }

    第一种注解方式,需要一个xml配置。接口都不变,只是在实现类上有些变化

     1 /*
     2 * dao层实现类
     3 */
     4 @Repository("dao")//相当于xml中配置的dao层的bean节点
     5 public class AccountDaoImpl implements AccountDao {
     6     @Override
     7     public void say() {
     8         System.out.println("我是dao层>>>>>>>>>>>>>");
     9     }
    10 }
    /**
     * service实现类
     */
    @Service("service")//相当于xml层中配置的service的bean节点
    public class AccountServiceImpl implements AccountService {
        @Autowired//默认按类型匹配
        @Qualifier("dao")//与autowired连用可以按名字匹配
        private AccountDao dao;//创建dao层成员,并写出它的set,get方法
    
        @Override
        public void say() {
            dao.say();
            System.out.println("我是service层>>>>>>>>>>>>>");
        }
    
        //get方法
        public AccountDao getDao() {
            return dao;
        }
        //set方法
        public void setDao(AccountDao dao) {
            this.dao = dao;
        }
    }

     xml配置:

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4        xmlns:context="http://www.springframework.org/schema/context"
     5        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">
     6     <!--扫描包-->
     7     <context:component-scan base-package="com.lhf"/>
     8     <!--开启ioc注解-->
     9     <context:annotation-config/>
    10 </beans>

    测试类不变,自行测试

    接下来最后一种就是不需要xml但是需要一个核心配置类AppConfig,取代xml

    1 @Configuration//标识,代表本类为核心配置类
    2 @ComponentScan("com.lhf")//需要扫描的包的位置
    3 public class AppConfig {
    4     //暂时什么都不用写,后续又其他操作
    5 }

    接口和实现类与上一种相同但是测试类略有变化

     1 public class App 
     2 {
     3     public static void main( String[] args )
     4     {
     5         //spring读取核心配置类
     6         ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
     7         //从spring容器中获取service层的对象
     8         AccountService bean = context.getBean(AccountService.class);
     9         //调用service层的方法
    10         bean.say();
    11     }
    12 }

    以上就是spring中对属性ioc对对象注入的几种方式。这里写的不全,有需要的可以查以下官方文档。

  • 相关阅读:
    Python3 简单实现下载某个网站轮播图片
    java中i=i++的解析
    String、StringBuffer、StringBuilder的区别和解析
    java值传递和引用传递
    Java并发(多线程)
    Java Set集合(HashSet、TreeSet)
    Java泛型的基本介绍与使用
    linux自学
    移动端项目准备工作
    响应式布局概念
  • 原文地址:https://www.cnblogs.com/Tiandaochouqin1/p/10387213.html
Copyright © 2020-2023  润新知