<?xml version=
<beans xmlns=
xmlns:xsi=
xmlns:p=
xsi:schemaLocation=
xmlns:context=
<context:annotation
<bean id=
</bean>
<bean id=
</bean>
<bean id=
</bean>
</beans>
User
package annotation
public class User {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this
}
}
UserDAO
package annotation
public interface UserDAO {
public void save(User user)
}
UserDAOImpl
package annotation
public class UserDAOImpl implements UserDAO {
public void save(User user) {
System
}
}
UserService
package annotation
import org
import org
public class UserService {
private UserDAO userDAO;
public void init() {
System
}
public void add(User user) {
userDAO
}
public UserDAO getUserDAO() {
return userDAO;
}
@Autowired//需配置在setter方法上
public void setUserDAO(@Qualifier(
this
}
public void destroy() {
System
}
}
UserServiceTest
package annotation
import org
import orgntext
import orgntext
public class UserServiceTest {
@Test
public void testAnnotationAutoWire() throws Exception {
ApplicationContext ctx = new ClassPathXmlApplicationContext(
UserService service = (UserService)ctx
service
System
}
}
From:http://tw.wingwit.com/Article/program/Java/ky/201311/28149.html