Configure annotation-autowire for spring

Source: Internet
Author: User

<? 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: p = "http://www.springframework.org/schema/p" xsi: schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xs D "xmlns: context =" http://www.springframework.org/schema/context "> <! -- To use annotation, add the content in three rows and the following line --> <context: annotation-config/> <bean id = "u" class = "annotation. autoWire. userDAOImpl "> </bean> <bean id =" u2 "class =" annotation. autoWire. userDAOImpl "> </bean> <bean id =" userService "class =" annotation. autoWire. userService "> </bean> </beans> User, java package annotation. autoWire; public class User {private String username; private String password; public String getUsername () {r Eturn username;} public void setUsername (String username) {this. username = username;} public String getPassword () {return password;} public void setPassword (String password) {this. password = password;} UserDAO. java package annotation. autoWire; public interface UserDAO {public void save (User user);} UserDAOImpl. java package annotation. autoWire; public class UserDAOImpl implements UserDAO {public void s Ave (User user) {System. out. println ("user saved! ") ;}} UserService. java package annotation. autoWire; import org. springframework. beans. factory. annotation. autowired; import org. springframework. beans. factory. annotation. qualifier; public class UserService {private UserDAO userDAO; public void init () {System. out. println ("init");} public void add (User user) {userDAO. save (user);} public UserDAO getUserDAO () {return userDAO;} @ Autowired // public void setUserDAO (@ Qualifier ("u2") UserDAO must be configured on the setter method) {this. userDAO = userDAO;} public void destroy () {System. out. println ("destroy") ;}} UserServiceTest. java package annotation. autoWire; import org. junit. test; import org. springframework. context. applicationContext; import org. springframework. context. support. classPathXmlApplicationContext; public class UserServiceTest {@ Testpublic void testAnnotationAutoWire () throws Exception {ApplicationContext ctx = new ClassPathXmlApplicationContext ("annotation/autoWire/annotationAutoWire. xml "); UserService service = (UserService) ctx. getBean ("userService"); service. add (new User (); System. out. println (service. getUserDAO ());}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.