使用Bean标签的注入方式
1.设置注入
<bean id="author" class="aop.cn.Author">
<property name="name" value="魏琦琦"/>
<property name="age" value="19"/>
<property name="sex" value="女"/>
</bean>
此方法为设置注入,通过无参构造方法实例化对象,并调用setter方法为属性赋值,如果没有无参方法,那么在映射文件xml里将会报错。
2.构造注入
<bean id="demo3" class="aop.cn.Demo">
<constructor-arg><value >tom</value></constructor-arg>
<constructor-arg><value type="int">3</value></constructor-arg>
</bean>
在Spring配置文件中通过元素为构造方法传参,通过有参构造方法匹配并实例化对象赋值,并且提供了type属性来指定参数类型,避免字符串类型与基本数据类型混淆。
3.p命名空间注入:Spring配置文件从2.0版本开始采用schema形式,使用不同的命名空间管理不同类型的配置,使得配置文件更具拓展性并且简化配置的工作量。
注意:使用p命名空间必须导入 xmlns:p=“http://www.springframework.org/schema/p”
<bean id="demo4" class="aop.cn.Demo" p:id="2" p:name="老七"/>
可以通过p:id和p:name直接为id和name属性赋值
处理不同类型的数据
- 特殊字符串处理
- JavaBean
- List
- Array
- Set
- Map
- Properties
- 空字符串
- null值
用户类User
public class User implements java.io.Serializable {
private String username; // 用户名
public User(){}
public User(String s, String s2, String s23) {
}
// getter & setter
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
实体类TestEntity
public class TestEntity {
private String specialCharacter1; // 特殊字符值1
private String specialCharacter2; // 特殊字符值2
private User innerBean; // JavaBean类型
private List<String> list; // List类型
private String[] array; // 数组类型
private Set<String> set; // Set类型
private Map<String, String> map; // Map类型
private Properties props; // Properties类型
private String emptyValue; // 注入空字符串值
private String nullValue = "init value"; // 注入null值
public void setSpecialCharacter1(String specialCharacter1) {
this.specialCharacter1 = specialCharacter1;
}
public void setSpecialCharacter2(String specialCharacter2) {
this.specialCharacter2 = specialCharacter2;
}
public void setInnerBean(User user) {
this.innerBean = user;
}
public void setList(List<String> list) {
this.list = list;
}
public void setArray(String[] array) {
this.array = array;
}
public void setSet(Set<String> set) {
this.set = set;
}
public void setMap(Map<String, String> map) {
this.map = map;
}
public void setProps(Properties props) {
this.props = props;
}
public void setEmptyValue(String emptyValue) {
this.emptyValue = emptyValue;
}
public void setNullValue(String nullValue) {
this.nullValue = nullValue;
}
public void showValue() {
System.out.println("特殊字符1:" + this.specialCharacter1);
System.out.println("特殊字符2:" + this.specialCharacter2);
System.out.println("内部Bean:" + this.innerBean.getUsername());
System.out.println("List属性:" + this.list);
System.out.println("数组属性[0]:" + this.array[0]);
System.out.println("Set属性:" + this.set);
System.out.println("Map属性:" + this.map);
System.out.println("Properties属性:" + this.props);
System.out.println("注入空字符串:[" + this.emptyValue + "]");
System.out.println("注入null值:" + this.nullValue);
}
}
Spring配置文件spring_config.xml
<bean id="entity" class="duo.TestEntity">
<!--使用<![CDATA[P&G]]]><![CDATA[P&G]]]>标记处理xml特殊字符-->
<property name="specialCharacter1">
<value><![CDATA[P&G]]]></value>
</property>
<!--把xml特殊字符替换为实体引用-->
<property name="specialCharacter2" >
<value>P&G</value>
</property>
<!--定义内部Bean-->
<property name="innerBean">
<bean class="duo.User">
<property name="username">
<value>Mr.Inner</value>
</property>
</bean>
</property>
<!--注入List类型-->
<property name="list">
<list>
<!--定义list中的元素-->
<value>足球</value>
<value>篮球</value>
</list>
</property>
<!--注入数组类型-->
<property name="array">
<list>
<value>足球</value>
<value>篮球 </value>
</list>
</property>
<!--Set类型-->
<property name="set">
<set>
<value>足球</value>
<value>篮球 </value>
</set>
</property>
<!--Map类型-->
<property name="map">
<map>
<!--定义Map中的键值对-->
<entry>
<key>
<value>football</value>
</key>
<value>足球</value>
</entry>
<entry>
<key>
<value>basketball</value>
</key>
<value>篮球</value>
</entry>
</map>
</property>
<property name="props">
<props>
<!--定义properties中的键值对-->
<prop key="football">足球</prop>
<prop key="basketball">篮球</prop>
</props>
</property>
<!--注入空字符串值-->
<property name="emptyValue">
<value></value>
</property>
<!--注入null值-->
<property name="nullValue">
<null/>
</property>
</bean>
</beans>
编写测试类
@Test
public void TestEtity() {
ApplicationContext context = new ClassPathXmlApplicationContext("spring_config.xml");
TestEntity testEtity = (TestEntity) context.getBean("entity");
testEtity.showValue();
}
测试结果
使用注解实现Ioc
spring提供的注解:
@Component:实现Bean组件的定义
@Repository :用于标注DAO类
@Service :用于标注业务类
@Controller :用于标注控制器类
定义一个Dao包接口UserDao
public interface UserDao {
public String getPwd(String name);
}
编写UserDao实现类
@Repository("userDao")
public class UserDaoImp implements UserDao {
@Override
public String getPwd(String name) {
//忽略数据库操作
return "amdin";
}
}
Service层创建UserService接口
public interface UserService {
public String getPwd(String name);
}
编写UserService实现类UserServiceImpl
@Service("userService")
public class UserServiceImpl implements UserService {
// @Autowired
// @Qualifier("userDao")
@Resource
private UserDao userDao;
@Override
public String getPwd(String name) {
return userDao.getPwd(name);
}
}
编写测试类
@Test
public void testzhujie() {
ApplicationContext context = new ClassPathXmlApplicationContext("spring_zhujie.xml");
UserService userService = (UserService) context.getBean("userService");
String pwd = userService.getPwd("admin");
System.out.println(pwd);
}
测试结果