首先说个用到spring3需要配的一个细节,这个不重要,但是在写配置的时候会感觉舒服点,就是xsd,
xsd简单说就是一个定义xml文档的合法构建模块,类似以前的dtd,其全称是 xml schema definition。
然而在xml中指定一个xsd通常在第一节点中,例如下面是配置spring3 annotation 的官方例子,
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config/>
</beans>
相对于spring的基本xml配置中多了2个部分,一个是xmlns:context..和下面两行context,因为我门在annotation中需要配置<context:annotation-config/>这样一句话。
接下来进入正题,如下有spring配置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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config />
<bean name="u" class="com.springdemo.dao.impl.UserDaoImpl">
<property name="id" value="1" />
</bean>
<bean id="userService" class="com.springdemo.service.impl.UserService" scope="prototype">
</bean>
</beans>
和被注入的UserService类,其中属性dao需要被注入:
package com.springdemo.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import com.springdemo.dao.IUserDao;
import com.springdemo.service.IUserService;
public class UserService implements IUserService {
//需要被注入
protected IUserDao dao;
public IUserDao getDao() {
return dao;
}
@Autowired
public void setDao(IUserDao dao) {
this.dao = dao;
}
@Override
public void add() {
// TODO Auto-generated method stub
dao.add();
}
@Override
public void del() {
// TODO Auto-generated method stub
dao.del();
}
@Override
public void update() {
// TODO Auto-generated method stub
dao.update();
}
}
和注入的IUserDao接口实现类:
package com.springdemo.dao.impl;
import com.springdemo.dao.IUserDao;
public class UserDaoImpl implements IUserDao {
private String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@Override
public void add() {
// TODO Auto-generated method stub
System.out.println("UserDaoImpl.add()");
System.out.println("id=" + id);
}
@Override
public void del() {
// TODO Auto-generated method stub
System.out.println("UserDaoImpl.del()");
System.out.println("id=" + id);
}
@Override
public void update() {
// TODO Auto-generated method stub
System.out.println("UserDaoImpl.update()");
System.out.println("id=" + id);
}
}
其中有name为u的bean 需要注入到userService 的属性dao中,通常我们不用annotation的时候自动装配都是在userService中添加autowire,其值通常byName或byType;
使用注解就不用这样配置,直接到userService的类中, 需要注解的setter上加上注解@AutoWired 即可 它等同于在xml上添加autowire="byType";
如果需要制定注入的bean 可以在setter的形参上加上@Qualifiler("u"),其中u为注入bean的name,代码如下
@Autowired
public void setDao(@Qualifiler("u")IUserDao dao) {
this.dao = dao;
}
再是@Require
故名思意,必须的,
This annotation simply indicates that the affected bean property must be populated at configuration time, through an explicit property value in a bean definition or through autowiring. The container throws an exception if the affected bean property has not been populated; this allows for eager and explicit failure, avoiding NullPointerExceptions or the like later on. It is still recommended that you put assertions into the bean class itself, for example, into an init method. Doing so enforces those required references and values even when you use the class outside of a container.
在文档中说到:必须要通过明确的bean定义或者通过自动装配来注入它,如果没有注入,它会在提前进行显示失败,而避免NullPointerExceptions 或者类似的报错。
@Require
public void setDao(IUserDao dao) {
this.dao = dao;
}