spring3学习笔记(六)bean属性的注入

2018-03-19  本文已影响0人  菜鸟要逆袭
什么是bean属性的注入

在创建对象的时候,向对象中注入相应的值

属性注入的方式
public class User{
  private String name;
  public void setName(){
    this.name=name;
  }
}
public class UserTest{
  User user = new User();
  user.setName("hobbes");
}
public class User{
  private String name;
  public User(String name){
    this.name=name;
  }
}
public class UserTest{
  User user = new User("hobbes");
}
public interface User{
  public void userFunction();
}
public class UserImpl implements User{
  public String name;
  public void userFunction(String name){
    this.name=name;
  }
}
spring框架中的属性注入

spring框架支持上一点中提到的set方法注入(重点)有参构造方法注入

//在测试类中获得该对象,并获得对象属性和方法
public class Test{
@Test
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
User user = (User)context.getBean("user");
user.userFunction();
}
//输出结果
userFuntion方法执行,username:hobbes

//需要被创建对象的类
public class User{
  private String username;
  public User(String username){
    this.username=username;
  }
  public void userFunction(){
    System.out.print("userFuntion方法执行,username:"+username);
  }
}

//在核心配置文件中对该类进行注册
<bean id="user" class="com.hobbes.test.User">
  <!-- 使用有参构造注入,name标签对应属性名,value标签对应注入的值 -->
  <constructor-arg name="username" value="hobbes"></constructor-arg>
</bean>

//在测试类中获得该对象,并获得对象属性和方法
public class Test{
  @Test
  ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
  User user = (User)context.getBean("user");
  user.userFunction();
}

//输出结果
userFuntion方法执行,username:hobbes
注入复杂类型属性的操作

上一点我们提到了对字符串类型的属性进行注入,但在实际开发过程中,我们经常会将类的对象、复杂的类型作为属性注入。接下来就对类的对象等复杂类型的注入做详解。

/*需求:在UserService类中调用UserDao类中的userFuntion方法*/
//创建需要被注入属性的类
public class UserService{
  //声明需要被注入的类的对象
  private UserDao userDao;
  //生成UserDao的set方法
  public void setUserDao(UserDao userDao){
    this.userDao = userDao;
  }
  public void userFunction(){
    system.out.println("UserService类中的userFunction方法执行了");
  }
}

//创建需要被当做对象属性注入UserService类中的类
public class UserDao{
  public void userFunction(){
    system.out.println("UserDao类中的userFunction方法执行了");
  }
}

//在核心配置文件中进行注入
<!-- 注册UserDao类 -->
<bean id="userDao" class="com.hobbes.test.UserDao"></bean>
<!-- 注册UserService类 -->
<bean id="userService" class="com.hobbes.test.UserService">
  <!-- 注入UserDao类的对象,其中ref标签的值对应被注入对象bean的id值 -->
  <property name="userDao" ref="userDao"></property>
</bean>

//在测试类中获得该对象,并获得对象属性和方法
public class Test{
  @Test
  ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
  UserService userServicec = (UserService)context.getBean("userService");
  userService.userFunction();
}

//输出结果
UserService类中的userFuntion方法执行了
UserDao类中的userFunction方法执行了
复杂类型对象的注入
//创建需要被创建的类
public class Person{
  private String pname;
  private String pname;
  public void setPname(String pname) {
    this.pname = pname;
  }
    
  private String[] arrs;
  private List<String> list;
  private Map<String,String> map;
  private Properties properties;
    
  public void setArrs(String[] arrs) {
    this.arrs = arrs;
  }
  public void setList(List<String> list) {
    this.list = list;
  }
  public void setMap(Map<String, String> map) {
    this.map = map;
  }
  public void setProperties(Properties properties) {
    this.properties = properties;
  }

  public void personTest() {
    System.out.println("arrs:"+arrs);
    System.out.println("list:"+list);
    System.out.println("map:"+map);
    System.out.println("properties"+properties);
  }
}

//在核心配置文件中注入相应的属性值
<bean id="person" class="com.hobbes.test.Person">
  <!-- 数组 -->
  <property name="arrs">
    <list>
      <value>张一</value>
      <value>张二</value>
      <value>张三</value>
    </list>
  </property>
        
  <!-- list -->
  <property name="list">
    <list>
      <value>张一</value>
      <value>张二</value>
      <value>张三</value>
    </list>         
  </property>
        
  <!-- map -->
  <property name="map">
    <map>
      <entry key="one" value="张一"></entry>
      <entry key="two" value="张二"></entry>
      <entry key="three" value="张三"></entry>
    </map>
  </property>
        
  <!-- properties -->
  <property name="properties">
    <props>
      <prop key="driverclass">com.mysql.jdbc.Driver</prop>
      <prop key="username">root</prop>
    </props>
  </property>
</bean>

//在测试类中获得该对象,并获得对象属性和方法
public class Test{
  @Test
  ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
  Person person = (Person )context.getBean("person");
  person.personTest ();
}

//输出结果
arrs:[Ljava.lang.String;@1865265]
list:[张一, 张二, 张三]
map:[one=张一, two=张二, three=张三]
properties:{driverclass=com.mysql.jdbc.Driver, username=root}
总结
上一篇下一篇

猜你喜欢

热点阅读