在类名前面加@Component进行实例化类、在beans.xml中用构造函数实例化对象——普通值、数组,list,set、Map和Properties、自定义类型实例化、给自定义类型初始化数据、给自定义类型初始化数据——推荐引用方式、接口注入实现类(实现接口回调)重点啊!!!

SpringMvcSSM框架

源码获取github


  • 1.目录结构(需要的jar包均在环境搭建中有)
  • 2.在类名前面加@Component进行实例化类
  • 3.在beans.xml中用构造函数实例化对象——普通值
  • 4.构造函数实例化数组,list,set
  • 5.构造函数实例化Map和Properties
  • 6.初始化自定义类型
  • 7.构造函数实例化自定义类型
  • 8.推荐引用方式——构造函数实例化自定义类型
  • 9.构造函数实现接口回调,接口注入实现类(重点!!!)


1.目录结构(需要的jar包均在环境搭建中有)

2.在类名前面加@Component进行实例化类

@Component//(value = “hsUser”) //相当于 (value = “hsUser”)就是id=hsUser

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"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
      http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">

     <!--1.启动Spring注解-->
    <context:annotation-config/>
    <!--2.扫描注解-->
    <context:component-scan base-package="com.hs"/> 

</beans>

User.java

package com.hs.model;

import org.springframework.stereotype.Component;

import java.util.*;

@Component//(value = "hsUser")  //相当于<bean class="com.hs.model.User" id="user"/> (value = "hsUser")就是id=hsUser
public class User {

   private String user_name;
   private Integer user_id;
   private String[] hobbyArray;
   private List<String> hobbyList;
   private Set<String> hobbySet;
   private Map<String, Object> map;
   private Properties properties;  //Map<string,string>

   private Role hsRole;    //这是自定义类型

   private ITest hsTest;   //定义接口变量

   /**
    * 这里通过bean.xml构造函数初始化
    * 1.ITestImpl testImpl = new ITestImpl();
    * 2.hsTest = testImpl
    * 3.ITest hsTest = new ITestImpl();
    * 典型的接口回调嘛
    * @param hsTest
    */
   public User(ITest hsTest) {
      this.hsTest = hsTest;
   }

   public User(Role hsRole) {
      this.hsRole = hsRole;
   }

   private User() {
   }

   public User(Map<String, Object> map, Properties properties) {
      this.map = map;
      this.properties = properties;
   }

   public User(Integer user_id, String user_name) {
      this.user_name = user_name;
      this.user_id = user_id;
   }

   public User(String user_name, Integer user_id, String[] hobbyArray, List<String> hobbyList, Set<String> hobbySet) {
      this.user_name = user_name;
      this.user_id = user_id;
      this.hobbyArray = hobbyArray;
      this.hobbyList = hobbyList;
      this.hobbySet = hobbySet;
   }

   public String getUser_name() {
      return user_name;
   }

   public void setUser_name(String user_name) {
      this.user_name = user_name;
   }

   public Integer getUser_id() {
      return user_id;
   }

   public void setUser_id(Integer user_id) {
      this.user_id = user_id;
   }

   public String[] getHobbyArray() {
      return hobbyArray;
   }

   public void setHobbyArray(String[] hobbyArray) {
      this.hobbyArray = hobbyArray;
   }

   public List<String> getHobbyList() {
      return hobbyList;
   }

   public void setHobbyList(List<String> hobbyList) {
      this.hobbyList = hobbyList;
   }

   public Set<String> getHobbySet() {
      return hobbySet;
   }

   public void setHobbySet(Set<String> hobbySet) {
      this.hobbySet = hobbySet;
   }

   public Map<String, Object> getMap() {
      return map;
   }

   public void setMap(Map<String, Object> map) {
      this.map = map;
   }

   public Properties getProperties() {
      return properties;
   }

   public void setProperties(Properties properties) {
      this.properties = properties;
   }

   public Role getHsRole() {
      return hsRole;
   }

   public void setHsRole(Role hsRole) {
      this.hsRole = hsRole;
   }

   public ITest getHsTest() {
      return hsTest;
   }

   public void setHsTest(ITest hsTest) {
      this.hsTest = hsTest;
   }

   @Override
   public String toString() {
      return "User{" +
            "user_name='" + user_name + '\'' +
            ", user_id=" + user_id +
            ", hobbyArray=" + Arrays.toString(hobbyArray) +
            ", hobbyList=" + hobbyList +
            ", hobbySet=" + hobbySet +
            ", map=" + map +
            ", properties=" + properties +
            ", hsRole=" + hsRole +
            ", hsTest=" + hsTest +
            '}';
   }
}

ConstructorObjectTest.java

/**
 * 测试在类名前面加@Component进行实例化类
 */
@Test
public void test01() {
   ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");

   User user = ac.getBean("user",User.class);
   System.out.println(user);
}

3.在beans.xml中用构造函数实例化对象——普通值

在spring中实例化对象,把类的构造函数设置成private,依然可以被实例化,在java中就不能被实例化

只要见到标签就是实例化。。。

User.java

public User(Integer user_id, String user_name) {
      this.user_name = user_name;
      this.user_id = user_id;
   }

beans.xml

<!--User 类通过构造函数实例化-->
<bean id="user01" class="com.hs.model.User">
   <!--index=""(可以省略)是设置参数的索引(0开始),是第几个 type可以不用写
        name就是构造函数的参数值,value就是赋的值
-->
   <!--翻译constructor:构造函数-->
   <constructor-arg name="user_id" value="100" index="0" type="java.lang.Integer"/>
   <constructor-arg name="user_name" index="1">
      <value>悟空</value>  <!--上面是简写-->
      <!--设置空是<null/>-->
   </constructor-arg>
</bean>

ConstructorObjectTest.java

/**
 * 测试在beans.xml中用构造函数实例化对象——普通值
 */
@Test
public void test02() {
   ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");

   User user = ac.getBean("user01",User.class);
   System.out.println(user);
}

4.构造函数实例化数组,list,set

User.java

public User(String user_name, Integer user_id, String[] hobbyArray, List<String> hobbyList, Set<String> hobbySet) {
   this.user_name = user_name;
   this.user_id = user_id;
   this.hobbyArray = hobbyArray;
   this.hobbyList = hobbyList;
   this.hobbySet = hobbySet;
}

beans.xml

<!--构造函数实例化数组,list,set-->
<bean id="user02" class="com.hs.model.User">
   <constructor-arg name="user_name" value="八戒" index="0"/>
   <constructor-arg name="user_id" value="200" index="1"/>
   <constructor-arg name="hobbyArray" index="2">
      <array>
         <value>篮球</value>
         <value>足球</value>
         <value>看书</value>
         <value>音乐</value>
      </array>
   </constructor-arg>
   <constructor-arg name="hobbyList">
      <list>
         <value>篮球</value>
         <value>足球</value>
         <value>看书</value>
         <value>音乐</value>
      </list>
   </constructor-arg>
   <constructor-arg name="hobbySet">
      <set>
         <value>篮球</value>
         <value>足球</value>
         <value>看书</value>
         <value>音乐</value>
      </set>
   </constructor-arg>
</bean>

ConstructorObjectTest.java

/**
 * 测试在beans.xml中用构造函数实例化对象——数组,list,set
 */
@Test
public void test03() {
   ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");

   User user = ac.getBean("user02",User.class);
   System.out.println(user);
}

5.构造函数实例化Map和Properties

User.java

public User(Map<String, Object> map, Properties properties) {
   this.map = map;
   this.properties = properties;
}

beans.xml

<!--构造函数实例化Map,Properties-->
<bean id="user03" class="com.hs.model.User">
   <constructor-arg name="map">
      <map>
         <entry key="hs">
            <value>和尚</value>
         </entry>
         <!--缩写-->
         <entry key="name" value="悟空"/>
      </map>
   </constructor-arg>
   <constructor-arg name="properties">
      <props>
         <!--给Properties赋值-->
         <prop key="age">100</prop>
         <prop key="RuntimeExceiption">error</prop>
      </props>
   </constructor-arg>
</bean>

测试

/**
 * 测试在beans.xml中用构造函数实例化对象——Map和Properties
 */
@Test
public void test04() {
   ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");

   User user = ac.getBean("user03",User.class);
   System.out.println(user);
}

6.初始化自定义类型

Role.java

package com.hs.model;

public class Role {

   private String role_name;

   public Role() {
   }

   public Role(String role_name) {
      this.role_name = role_name;
   }

   public String getRole_name() {
      return role_name;
   }

   public void setRole_name(String role_name) {
      this.role_name = role_name;
   }

   @Override
   public String toString() {
      return "Role{" +
            "role_name='" + role_name + '\'' +
            '}';
   }
}

User.java

private Role hsRole;    //这是自定义类型

beans.xml

<!--构造函数初始化自定义类型-->
<bean id="user04" class="com.hs.model.User">
   <constructor-arg name="hsRole">
      <bean id="role1" class="com.hs.model.Role"/>
   </constructor-arg>
</bean>

测试

/**
 * 自定义类型实例化,给User类中的Role类的变量,进行实例化操作
 */
@Test
public void test05() {
   ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");

   User user = ac.getBean("user04",User.class);
   System.out.println(user);
   //下面这个证明User类的那个bean Role role,范围只能在User里
   /*Role role1 = ac.getBean("role1", Role.class);
   System.out.println(role1);*/
}

7.构造函数实例化自定义类型

Role.java

public Role(String role_name) {
   this.role_name = role_name;
}

User.java

public User(Role hsRole) {
   this.hsRole = hsRole;
}

beans.xml

<!--构造函数实例化自定义类型-->
<bean id="user05" class="com.hs.model.User">
   <constructor-arg name="hsRole">
      <bean class="com.hs.model.Role">
         <constructor-arg name="role_name" value="超级管理员"/>
      </bean>
   </constructor-arg>
</bean>

测试

/**
     * 给自定义类型初始化数据
     */
    @Test
    public void test06() {
        ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");

        User user = ac.getBean("user05",User.class);
        System.out.println(user);
    }

8.推荐引用方式——构造函数实例化自定义类型

beans.xml

<!--构造函数实例化自定义类型——推荐引用方式-->
<bean id="user06" class="com.hs.model.User">
   <!--<constructor-arg name="hsRole">
      <!–引用bean,下面简写–>
      <ref bean="role2"/>
   </constructor-arg>-->
   <constructor-arg name="hsRole" ref="role2"/>
</bean>
<bean id="role2" class="com.hs.model.Role">
   <constructor-arg name="role_name" value="引用方式推荐"/>
</bean>

测试

/**
 * 给自定义类型初始化数据——推荐引用方式
 */
@Test
public void test07() {
   ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");

   User user = ac.getBean("user06",User.class);
   System.out.println(user);
   Role role = ac.getBean("role2", Role.class);
   System.out.println(role);
}

9.构造函数实现接口回调,接口注入实现类(重点!!!)

ITest.java

package com.hs.model;

public interface ITest {

   void print();
}

ITestImpl.java

package com.hs.model;

public class ITestImpl implements ITest {

   @Override
   public void print() {
      System.out.println("Hello Word!!");
   }
}

User.java

private ITest hsTest;   //定义接口变量

/**
 * 这里通过bean.xml构造函数初始化
 * 1.ITestImpl testImpl = new ITestImpl();
 * 2.hsTest = testImpl
 * 3.ITest hsTest = new ITestImpl();
 * 典型的接口回调嘛
 * @param hsTest
 */

public User(ITest hsTest) {
   this.hsTest = hsTest;
}

beans.xml

<!--实现接口回调,接口注入实现类-->
<!-- 这里通过bean.xml构造函数初始化
 1.ITestImpl testImpl = new ITestImpl();
 2.hsTest = testImpl -->
<bean id="user07" class="com.hs.model.User">
   <constructor-arg name="hsTest">
      <ref bean="testImpl"/>
   </constructor-arg>
</bean>
<!--ITestImpl testImpl = new ITestImpl();-->
<bean id="testImpl" class="com.hs.model.ITestImpl"/>

测试

/**
 * 接口注入实现类,实现接口回调
 */
@Test
public void test08() {
   ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");

   User user = ac.getBean("user07",User.class);
   user.getHsTest().print();
}