zoukankan      html  css  js  c++  java
  • spring06

    主要是学会spring中关于通过注释来配置bean。

    以及处理怎样智选其一或者补选某一个bean还有就是关于当出现多个相同的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:context="http://www.springframework.org/schema/context"
        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-4.3.xsd">
        <!-- 指定spring ioc容器扫描的包 -->   
        <!-- 通过resource-pattern来指定扫描的资源 -->
      <!-- <context:component-scan base-package="test1" 
        resource-pattern="test1.user/*.class">
        </context:component-scan>-->
         <context:component-scan base-package="test1" use-default-filters="true">
       <!--   <context:exclude-filter type="annotation" expression="baoing"/>指定排除那些那些指定的表达式的组件--> 
      <!-- <context:include-filter type="annotation" expression="baoming "只包含指定的组件,需要将相应use-default-filters="false"的贼之改成false-->
      <!--    <context:exclude-filter type="assignable" expression="jikoubaomingjialeiming "/>-->
        </context:component-scan>
    
    </beans>

    然后就是相应的g各个bean文件以及上边相应的注释。;

    注意一定要放到相应的子包下下面。

    package test1;
    
    import org.springframework.stereotype.Component;
    
    @Component
    public class TestObject {
    
    }
    package test1.user;
    
    public interface UserRepository {
        void save();
    }
    package test1.user;
    
    import org.springframework.stereotype.Repository;
    
    @Repository("userRepository")
    public class UserReposityImpl implements UserRepository {
    
        @Override
        public void save() {
            // TODO Auto-generated method stub
            System.out.println("UserRepository save....");
    
        }
    }
    package test1;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import test1.cont.UserController;
    import test1.ser.UserService;
    import test1.user.UserRepository;
    
    public class Spring {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            ApplicationContext ct=new ClassPathXmlApplicationContext("beans-053.xml");
            TestObject to=(TestObject) ct.getBean("testObject");
            UserController userController=(UserController) ct.getBean("userController");
            UserRepository userRepository=(UserRepository) ct.getBean("userRepository");
            UserService userService=(UserService) ct.getBean("userService");
            System.out.println(to);
            System.out.println(userController);
            System.out.println(userService);
            System.out.println(userRepository);
        }
    
    }
    package test1.cont;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    
    import test1.ser.UserService;
    
    @Controller
    public class UserController {
        @Autowired
        private UserService userService;
    public void execute()
    {
        System.out.println("UserController execute..");
        userService.add();
    }
    }
    package test1.ser;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.stereotype.Service;
    
    import test1.user.UserRepository;
    //@Autowired(required=false)设置没有spring装备的bean
    //@Qualifier("zhidingde bean")设置指定的bean
    @Service
    public class UserService {
        @Autowired
        private UserRepository userRepository;
        public void add()
        {
            System.out.println("UserSerivce add..");
            userRepository.save();
        }
    
    }

    实验结果如下:

  • 相关阅读:
    [工作积累] shadow map问题汇总
    引擎设计跟踪(九.14.3.4) mile stone 2
    引擎设计跟踪(九.14.3.3) Deferred shading的一些小细节
    引擎设计跟踪(九.14.3.2) Deferred shading的后续实现和优化
    《口袋妖怪 太阳/月亮》正式公布 简体中文确认
    古墓丽影:崛起 PC版今日发售
    枪弹辩驳(弹丸论破)即将登陆PC
    引擎设计跟踪(九.14.3.1) deferred shading: Depthstencil as GBuffer depth
    引擎设计跟踪(九.14.3) deferred shading 准备
    引擎设计跟踪(九.14.2 final) Inverse Kinematics: CCD 在Blade中的实现
  • 原文地址:https://www.cnblogs.com/dazhi151/p/12642590.html
Copyright © 2011-2022 走看看