实现类:
public class ReadDaoImpl implements ReadDao {
private String str = "我是杂志";
public ReadDaoImpl(){
}
public ReadDaoImpl(String str){
this.str=str;
}
@Override
public void getString() {
System.out.println(str);
}
}
set方式实现
如果我们去掉实现类的无参构造方法,会报错。因此默认是无参构造的方式创建对象。
bean文件
<bean id="readDao" class="com.spring.daoimpl.ReadDaoImpl">
</bean>
<bean id="readService" class="com.spring.serviceimpl.ReadServiceImpl">
<property name="readDao" ref="readDao"></property>
</bean>
<bean id="readDao" class="com.spring.daoimpl.ReadDaoImpl">
<constructor-arg index="0" value="杂志"></constructor-arg>
</bean>
<bean id="readService" class="com.spring.serviceimpl.ReadServiceImpl">
<property name="readDao" ref="readDao"></property>
</bean>
<bean id="readDao" class="com.spring.daoimpl.ReadDaoImpl">
<constructor-arg type="int" value="1"></constructor-arg>
<constructor-arg type="java.lang.String" value="杂志1"></constructor-arg>
<constructor-arg type="java.lang.String" value="杂志"></constructor-arg>
</bean>
根据类型注入需要注意当类型一致时根据顺序依次注入因此不推荐使用
<bean id="readDao" class="com.spring.daoimpl.ReadDaoImpl">
<constructor-arg name="a" value="1"></constructor-arg>
<constructor-arg name="str" value="杂志1"></constructor-arg>
<constructor-arg name="str1" value="杂志"></constructor-arg>
</bean>
<alias name="userService" alias="userServiceNew"></alias>
顾名思义
UserService userService = (UserService) context.getBean("userService");
UserService userServiceNew = (UserService) context.getBean("userServiceNew");
上边代码获取的是同一个对象。
<bean id="userService" class="com.spring.serviceimpl.UserServiceImpl" name="s1 s2,s3;s4">
<property name="userDao1" ref="userDaoZs"></property>
<property name="userDao" ref="userDaoLs"></property>
</bean>
bean标签
property标签
导入,当我们联合开发通过导入将文件内容合并
<import resource="beans.xml"></import>