Spring Annotation AutoWiredAndQualifier

Use Annotation configuration

1. Import jar package: spring-aop-4.0.2.RELEASE.jar
or will be error Unexpected exception parsing XML document from class path resource [beans.xml]
2. Configure xml (you can view the api documentation)

<?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.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

    <context:annotation-config/>
	<bean id="UserDAO" class="com.login.dao.impl.UserDAOImpl"></bean>
	<bean id="UserDAO2" class="com.login.dao.impl.UserDAOImpl"></bean>
	<bean id="userService" class="com.login.service.UserService"></bean>
</beans>

AutoWiredAndQualifier

1.AutoWired
may be disposed on the attribute name may be disposed on the method, in order not to affect the encapsulation method is preferably arranged in the set. Automatic injection.

   @Autowired
	public void setUserDAO(@Qualifier("UserDAO") UserDAO userDAO) {
		this.userDAO = userDAO;
	}

2.Qualifier
Autowired achieve injection mode is the default byType, when Qualifier annotations can be achieved byName way of injection.

Published 47 original articles · won praise 5 · Views 2020

Guess you like

Origin blog.csdn.net/OVO_LQ_Start/article/details/104341729