zoukankan      html  css  js  c++  java
  • Spring集合注入和域属性自动注入byName和byType

    Spring集合的注入

      步骤一:导入依赖

    <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-beans</artifactId>
          <version>5.1.5.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context</artifactId>
          <version>5.1.5.RELEASE</version>
        </dependency>
        <!--切面-->
        <dependency>
          <groupId>org.aspectj</groupId>
          <artifactId>aspectjweaver</artifactId>
          <version>1.7.0</version>
        </dependency>

      步骤二:创建实体类

      

    public class Studenttest {
        //数组
        private String [] arrays;
        //list集合
        private List<Integer> liats;
        //set集合
        private Set<String> sets;
        //map集合
        private Map<String,Object> maps;
        //配置
        private Properties properties;
    
        @Override
        public String toString() {
            return "Studenttest{" +
                    "arrays=" + Arrays.toString(arrays) +
                    ", liats=" + liats +
                    ", sets=" + sets +
                    ", maps=" + maps +
                    ", properties=" + properties +
                    '}';
        }
    
        public String[] getArrays() {
            return arrays;
        }
    
        public void setArrays(String[] arrays) {
            this.arrays = arrays;
        }
    
        public List<Integer> getLiats() {
            return liats;
        }
    
        public void setLiats(List<Integer> liats) {
            this.liats = liats;
        }
    
        public Set<String> getSets() {
            return sets;
        }
    
        public void setSets(Set<String> sets) {
            this.sets = sets;
        }
    
        public Map<String, Object> getMaps() {
            return maps;
        }
    
        public void setMaps(Map<String, Object> maps) {
            this.maps = maps;
        }
    
        public Properties getProperties() {
            return properties;
        }
    
        public void setProperties(Properties properties) {
            this.properties = properties;
        }
    }

      步骤三:创建大配置文件

    <?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:aop="http://www.springframework.org/schema/aop"
           xmlns:p="http://www.springframework.org/schema/p"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop.xsd">
    
        <!--给集合属性赋值-->
        <bean id="diTest" class="com.SpringMckz.di.Studenttest">
            <property name="arrays">
                <array>
                    <value>梅</value>
                    <value>川</value>
                    <value>酷</value>
                    <value>子</value>
                </array>
            </property>
            <property name="liats">
                <list>
                    <value>1</value>
                    <value>2</value>
                    <value>3</value>
                    <value>4</value>
                </list>
            </property>
            <property name="sets">
                <set>
                    <value>1</value>
                    <value>2</value>
                    <value>3</value>
                    <value>4</value>
                </set>
            </property>
            <property name="maps">
                <map>
                    <entry key="阿斯顿" value="1234"></entry>
                    <entry key="阿1" value="21321"></entry>
                </map>
            </property>
            <property name="properties">
                <props>
                    <prop key="jdbc.driver">com.mysql.jdbc.Driver</prop>
                </props>
            </property>
        </bean>
    </beans>

      步骤四:测试

    @Test
        public void studenttest(){
            ApplicationContext atc=new ClassPathXmlApplicationContext("applicationContext.xml");
            Studenttest sss = (Studenttest)atc.getBean("diTest");
            System.out.println(sss.toString());
        }

    域属性自动注入

      byName与byType

      步骤一:创建两个实体类

    public class Student {
        private Integer stuid;
        private String stuName;
        private Teacher teacher;
    
        public Teacher getTeacher() {
            return teacher;
        }
    
        public void setTeacher(Teacher teacher) {
            this.teacher = teacher;
        }
    
        public Integer getStuid() {
            return stuid;
        }
    
        public void setStuid(Integer stuid) {
            this.stuid = stuid;
        }
    
        public String getStuName() {
            return stuName;
        }
    
        public void setStuName(String stuName) {
            this.stuName = stuName;
        }
    }
    public class Teacher {
        private Integer tid;
        private String tname;
    
        public Integer getTid() {
            return tid;
        }
    
        public void setTid(Integer tid) {
            this.tid = tid;
        }
    
        public String getTname() {
            return tname;
        }
    
        public void setTname(String tname) {
            this.tname = tname;
        }
    }

      步骤二:创建大配置文件(在bean节点中增加autowire属性,设值为byType)

      要求:给Teacher赋值的bean节点的id与域属性的名字相同

    <?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:aop="http://www.springframework.org/schema/aop"
           xmlns:p="http://www.springframework.org/schema/p"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop.xsd">
    
        <bean id="stuentity" class="com.SpringMckz.entity.Student" autowire="byName">
            <property name="stuid" value="123"></property>
            <property name="stuName" value="梅川酷子"></property>
        </bean>
        <bean id="teacher" class="com.SpringMckz.entity.Teacher">
            <property name="tid" value="250"></property>
            <property name="tname" value="超"></property>
        </bean>
    </beans>

      步骤四:测试(autowire="byName")

    @Test
        public void testsentitu(){
            ApplicationContext atc=new ClassPathXmlApplicationContext("applicationContext.xml");
            Student sss = (Student)atc.getBean("stuentity");
            System.out.println(sss.getTeacher().getTid());
            System.out.println(sss.getTeacher().getTname());
        }

       步骤五:测试(autowire="byType")

      要求:保证域属性的类型与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:aop="http://www.springframework.org/schema/aop"
           xmlns:p="http://www.springframework.org/schema/p"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop.xsd">
    
        <bean id="stuentity" class="com.SpringMckz.entity.Student" autowire="byType">
            <property name="stuid" value="123"></property>
            <property name="stuName" value="梅川酷子"></property>
        </bean>
        <bean id="teacher" class="com.SpringMckz.entity.Teacher">
            <property name="tid" value="250"></property>
            <property name="tname" value="超"></property>
        </bean>
    </beans>
    @Test
        public void testsentitu(){
            ApplicationContext atc=new ClassPathXmlApplicationContext("applicationContext.xml");
            Student sss = (Student)atc.getBean("stuentity");
            System.out.println(sss.getTeacher().getTid());
            System.out.println(sss.getTeacher().getTname());
        }

  • 相关阅读:
    使用线程加载指定目录下的所有子目录和文件名称到TreeView中
    《C++代码设计与重用》读后感
    无限分类编辑移动栏目代码
    查询数据库中所有记录总数不为0的数据表名称
    DES,RC2,Rijndael, TripleDes,RSA, MD5, SHA1通用的安全密码类
    SQL过滤掉空格和其他特殊字符函数
    @property详解:nonatomic,retain (转)
    UITextField控件处理键盘弹出时遮住输入框的问题
    iphone 定时器的使用
    苹果官方例子代码
  • 原文地址:https://www.cnblogs.com/whtt/p/11752429.html
Copyright © 2011-2022 走看看