Spring Framework - -type configuration injected Attribute Properties

Or use above a small demo, test, verify a problem, ado, on to compare the code

. 1  Package service.impl;
 2  
. 3  Import service.UserService;
 . 4  Import dao.UserDao;
 . 5  Import entity.User;
 . 6  
. 7  / ** 
. 8  * user traffic class, to achieve business management User functions
 . 9   * / 
10  public  class UserServiceImpl the implements UserService {
 . 11  
12 is      // declaration of the interface reference type, and decoupling implementation class 
13 is      Private UserDao DAO;
 14      Private String S1;
 15      Private String S2;
 16      Private  int I1;
. 17      
18 is  
. 19      @ generated constructor with no arguments 
20 is      public UserServiceImpl () {
 21 is  
22 is      }
 23 is  
24      // parameters as constructor assignment DAO 
25      public UserServiceImpl (UserDao DAO) {
 26 is          the this .dao = DAO;
 27      }
 28      public UserServiceImpl (DAO UserDao, String S1) {
 29          the this .dao = DAO;
 30          the this .s1 = S1;
 31 is      }
 32      public UserServiceImpl (String S2, UserDao DAO) {
 33 is          the this .dao =DAO;
 34 is          the this .s2 = S2;
 35      }
 36      public UserServiceImpl ( int I1, UserDao DAO) {
 37 [          the this .dao = DAO;
 38 is          the this .i1 = I1;
 39      }
 40  
41 is  
42 is      public UserDao getDao () {
 43 is          return DAO ;
 44 is      }
 45  
46 is      // the setter accessor dao attribute is invoked Spring, setter injection to achieve 
47      public  void setDao (UserDao dao) {
 48          the this .dao = dao;
 49     }
 50  
51 is      public  void AddNewUser (the User User) {
 52 is          // method calls the user's user information stored DAO 
53 is          dao.save (User);
 54 is          System.out.println ( "S1 =" + S1);
 55          the System.out. the println ( "S2 =" + S2);
 56 is          System.out.println ( "I1 =" + I1);
 57 is      }
 58 }

Core configuration file:

<bean id="UserService" class="service.impl.UserServiceImpl">
<!--通过构造函数的方式,实现构造注入 -->
<constructor-arg>
  <ref bean="userDao" />
</constructor-arg>
<constructor-arg>
  <value>123</value>
</constructor-arg>
</bean>

运行结果:

保存用户信息到数据库
s1=null
s2=123
i1=0

由此可见,是没有附上值的 ,为i1变量

解决办法:对核心配置文件进行修改,使用type属性,指定数据类型

 

 运行结果:

保存用户信息到数据库
s1=null
s2=null
i1=123

Guess you like

Origin www.cnblogs.com/dongyaotou/p/12115500.html