Spring Study ( One )—Spring Framwork introduction /IoC Thought and DI/ Annotation based injection
1. Spring Framwork introduction
2002 year ,Rod Johnson For the first time Spring Framwork The forerunner of Interface21.2003 year ,Spring Framwork The company uses Interface21 Based on , Redesigned and released Spring Framwork Of 1.0 Official version .
It is worth mentioning that Rod Johnson It’s not just software development , He also got A doctorate in Musicology .
Let’s see Spring Its official website https://spring.io/, This painting style can be said to be a very big artist .I like it~
On the official website Projects Can be found in Spring Framework, Click to enter , You can see right now Spring Framework It has been updated to 5.1.8 edition , Considering that the new version is generally not very stable , Choose here 4.3.9 Version to learn (emmmm… In fact, the teacher recommended this version , Probably because this version is widely used in enterprises ).
Official reference documents are attached https://docs.spring.io/spring/docs/4.3.9.RELEASE/spring-framework-reference/htmlsingle/
Git Escrow address :https://github.com/spring-projects/spring-framework
Now? , Start learning formally ~
First , Let’s first study ,Spring Framwork What is it ? That’s what the official documents say ,Spring Framework It’s a Java platform , For development Java Applications provide comprehensive infrastructure support .
Spring Framework It’s a layered architecture , from 7 Well defined modules make up . form Spring Every module of the framework ( Or component ) Can exist alone , Or in combination with one or more other modules .
The functions of each module are as follows :
- Spring Modules are built in the core container Spring Core above , The core container defines the creation of 、 Configure and manage bean The way . The main components of the core container are
BeanFactory
, It’s the implementation of the factory model .BeanFactory
Use Inversion of control (IOC) Pattern Separate application configuration and dependency specifications from the actual application code . - Spring Context Is a profile , towards Spring The framework provides context information .Spring Context includes enterprise services , for example JNDI、EJB、 E-mail 、 internationalization 、 Check and dispatch function .
- Spring AOP: Through configuration management features ,Spring AOP Module directly integrates aspect oriented programming functions into Spring In the frame . therefore , It’s easy to make Spring Any object support for framework management AOP.Spring AOP The module is based on Spring Objects in the application provide transaction management services . By using Spring AOP, Don’t rely on EJB Components , You can integrate declarative transaction management into your application .
- Spring DAO:JDBC DAO The abstraction layer provides meaningful Exception hierarchy , This structure can be used to Manage exception handling and error messages thrown by different database vendors . Exception hierarchy Simplified error handling , And greatly reduces the number of exception code to be written ( For example, opening and closing the connection ).Spring DAO Face of JDBC The exception of follows the general DAO Exception hierarchy .
- Spring ORM:Spring The frame inserts several ORM frame , Thus providing ORM Object relation tools , These include JDO、Hibernate and iBatis SQL Map. All of this follows Spring General affairs and DAO Exception hierarchy .
- Spring Web modular :Web The context module is built on top of the application context module , Based on Web The application provides context . therefore ,Spring Framework support and Jakarta Struts Integration of .Web The module also simplifies the processing of multipart requests and binding request parameters to domain objects .
- Spring MVC frame :Spring MVC (Model View Controller) Framework is a full-featured construction Web Application’s MVC Realization . Through the strategic interface ,MVC The frame becomes highly configurable ,MVC It contains a lot of view technology , These include JSP、Velocity、Tiles、iText and POI.
Most people are learning Spring Before , It should have been learned Mybatis, Take a look back. Mybatis What has been done for us ? Just to summarize ,MyBatis Through simple xml To configure Avoided Almost all JDBC Code and Manual setting of parameters , It can make Dao The layer focuses on the operation of the database , Focus on SQL Statement writing , get SQL Query results of . So next , Let’s see. Spring What can be done for us ?
The official document says , As an application developer , You can learn from Spring The following is an example of a platform that benefits :1. send Java Method is executed in a database transaction , You don’t have to deal with the business API.2. Make local Java The method becomes HTTP Endpoint , There’s no need to deal with Servlet API.3. Make local Java Method becomes a message handler , There’s no need to deal with JMS API.4. Make local Java Methods become management operations , There’s no need to deal with JMX API.
In short ,Spring It’s a lightweight implementation of inversion of control IoC And face the facet AOP Framework .
that , What is? IoC and AOP Well ?
By the most primitive way of development Monomer application
Speaking of : Here to operate the database user Form as background , We have to come from pojo Layers start to develop .
pojo layer : Write entity class corresponding to database User
package com.hooi.pojo;
public class User {
private String userName;
public User() {
}
public User(String userName) {
this.userName = userName;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
dao layer : To write UserDao Interface ( Abstract method of adding, deleting, modifying and searching ) and UserDaoImpl Implementation class ( The concrete realization of adding, deleting, modifying and searching )
package com.hooi.dao;
public interface UserDao {
// Get user information
public abstract void getUserInfo();
}
package com.hooi.dao.Impl;
import com.hooi.dao.UserDao;
public class UserDaoMysqlImpl implements UserDao {
public void getUserInfo() {
System.out.println(" obtain MySQL User information ");
}
}
Service layer : To write UserService Business interface ( And dao The method of layer corresponds to ) and UserServiceImpl Implementation class ( Implement business interface ), Implementation classes need to hold and dao Reference to the implementation class corresponding to the layer , Only then can we make Service Layers pass through Dao The implementation class of the layer calls the corresponding database operation method required by the business .
package com.hooi.service;
public interface UserService {
// Get user information
public abstract void getUserInfo();
}
package com.hooi.service;
import com.hooi.dao.UserDao;
public class UserServiceImpl implements UserService {
private UserDao userDao = new UserDaoMySqlImpl();//Dao Layer implementation class reference
// Get user information
public void getUserInfo() {
userDao.getUserInfo();// adopt Dao The instance of the layer implementation class calls the corresponding method
}
}
Servlet layer : The business layer implements the class method to process the user request from the front end .
UserServiceImpl service = new UserServiceImpl();
service.getUserInfo();
If dao Layer wants to develop a new UserDao Implementation class , for instance UserDaoOracleImpl
. Then the corresponding should be modified Service Layer implementation class holds Dao Implementation class reference private UserDao userDao = new UserDaoOracleImpl();
. If dao There are many implementation classes for layer extension ? Although for Servlet Layer , What to do ( The corresponding parameters are passed and called ) It doesn’t change , But in Service level , Programmers will need to do a lot of code modification and maintenance work . The requirements of the client are sent back by the front end ,servlet The layer can determine what is needed when processing the request UserDao Implementation class , But left a lot of work to Service Layer to deal with , And that leads to Service Layers can’t just focus on business processing , Contrary to the idea of decoupling . Let’s think about it , How to let the front end choose to use dao Which implementation class of the layer , Give Way Service The layer no longer manages the creation of related objects ? With the idea of encapsulation and interface oriented programming , We can do it in Service Stratification Servlet Layer leaves an interface ,Service Layer when using an implementation class , Not to create it , Instead, it refers to the interface implemented by the implementation class .UserServiceImpl Class can be modified to :
package com.hooi.service;
import com.hooi.dao.UserDao;
public class UserServiceImpl implements UserService {
private UserDao userDao;// Encapsulate the implementation class with an interface
public UserServiceImpl() {
}
public UserServiceImpl(UserDao userDao) {
this.userDao = userDao;
}
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public void getUserInfo() {
userDao.getUserInfo();
}
}
The concrete implementation class of the interface is defined by Servlet Layers pass through the corresponding set Method to set , No matter how the requirements of the front end change ,Service Layers don’t need a lot of code changes , Just focus on the business . and Servlet The operation of the layer only adds the steps of creating related objects .
package com.hooi.service;
import com.hooi.dao.Impl.UserDaoMysqlImpl;
import com.hooi.dao.Impl.UserDaoOracleImpl;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ServiceTest {
@Test
public void getUserInfo(){
UserServiceImpl service = new UserServiceImpl();
// If the front end needs Oracle User data for , stay servlet Layer for UserServiceImpl Set the corresponding implementation class properties
service.setUserDao(new UserDaoOracleImpl());
service.getUserInfo();
// If the front end needs Mysql User data for , stay servlet Layer for UserServiceImpl Set the corresponding implementation class properties
service.setUserDao(new UserDaoMysqlImpl());
service.getUserInfo();
}
}
however , even so , When we need to use another object in one object , We still need to get it , It needs to be destroyed after use ( such as Connection etc. ), These objects are always coupled to other interfaces or classes . imagine , If there’s a container that can Specifically responsible for managing the life cycle of objects and relationships between objects , When we want to use an object , The whole process is no longer under our own control , It’s done by this container , If the object the container gives us doesn’t meet our needs , We just throw an exception . and Spring The same is true of the development mode advocated by the government , Creation of all classes 、 Destroyed by Spring To control , It is no longer the object that references it that controls the life cycle of an object , It is Spring. For a specific object , It used to control the creation of other objects , And now all the objects are Spring control , Our programs have become more Highly configurable and dynamic , This is it. Spring The core idea of IoC(Inversion of Control), Inversion of control .
02 IoC and DI
IoC Depend on DI(Dependency Injection, Dependency injection ) Realization .
First , We use Spring The framework changes the above case . Use Spring The framework must be imported first jar package .
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
here pojo The layer still uses the above User Entity class ,Dao and Service Layer code remains unchanged . Next we need to write Spring Configuration file for . stay resources Create under directory applicationContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--User object -->
<bean id="user" class="com.hooi.pojo.User">
<property name="userName" value="hooi"/>
</bean>
<!--UserDaoMysqlImpl object -->
<bean id="userDaoMysqlImpl" class="com.hooi.dao.Impl.UserDaoMysqlImpl"/>
<!--UserDaoOracleImpl object -->
<bean id="userDaoOracleImpl" class="com.hooi.dao.Impl.UserDaoOracleImpl"/>
<!--MysqlService-->
<bean id="userServiceMysqlImpl" class="com.hooi.service.UserServiceImpl">
<property name="userDao" ref="userDaoMysqlImpl"/>
</bean>
<!--OracleService-->
<bean id="userServiceOracleImpl" class="com.hooi.service.UserServiceImpl">
<property name="userDao" ref="userDaoOracleImpl"/>
</bean>
</beans>
Use junit To test :
@Test
public void _getUserInfo(){
// adopt ClassPathXmlApplicationContext Enter the corresponding xml File to get the object you want
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserServiceImpl userServiceMysqlImpl = (UserServiceImpl) context.getBean("userServiceMysqlImpl");
userServiceMysqlImpl.getUserInfo();
UserServiceImpl userServiceOracleImpl = (UserServiceImpl) context.getBean("userServiceOracleImpl");
userServiceOracleImpl.getUserInfo();
}
test result :
Use Spring after , We don’t need to go again Servlet Layers or Service Layer changes a lot of code , When we want to implement different operations , Only in the corresponding xml Modify or re register new in the configuration file Bean. The objects used above are all made by Spring establish , management , assembly . that , How do the properties of these objects set their values ?
Next , Let’s explore DI(Dependency Injection, Dependency injection ).
We usually create objects in two ways : Parametric or nonparametric structures . that , So let’s test that out Spring How to implement dependency injection through a constructor .
First , modified User Entity class , To test .
package com.hooi.pojo;
public class User {
private String userName;
private String gender;
private int age;
public User() {
System.out.println(" No arguments structure ");
}
public User(String userName) {
System.out.println(" There are parametric structures ( One )");
this.userName = userName;
}
public User(String userName,String gender) {
System.out.println(" There are parametric structures ( Two )");
this.userName = userName;
this.gender = gender;
}
public User(String userName,String gender,int age) {
System.out.println(" There are parametric structures ( 3、 ... and )");
this.userName = userName;
this.gender = gender;
this.age = age;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
System.out.println("setUserName:"+userName);
this.userName = userName;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
System.out.println("setGender:"+gender);
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
System.out.println("setAge:"+age);
this.age = age;
}
@Override
public String toString() {
return "User{" +
"userName='" + userName + '\'' +
", gender='" + gender + '\'' +
", age=" + age +
'}';
}
}
Test code :
package com.hooi.pojo;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class UserTest {
@Test
public void userTest(){
System.out.println("-----------> Get context ");
ApplicationContext context = new ClassPathXmlApplicationContext("userBean.xml");
User user = (User) context.getBean("user");
System.out.println("-----------> Get objects "+user);
}
}
Spring The relevant configuration file userBean.xml
To configure 1:
<!-- Use property Assign values to member variables -->
<bean id="user" class="com.hooi.pojo.User">
<property name="userName" value="hooi"/>
</bean>
test result 1: It can be seen from the test results that property Attribute dependent construction and set Method
We will User Class without a parameter constructor. Note out , The test again , The test results are as follows : The default constructor is not found
To configure 2:
<!-- Attribute assignment using constructor's parameter subscript -->
<bean id="user" class="com.hooi.pojo.User">
<constructor-arg index="0" value="hooi2"/>
<constructor-arg index="1" value="female"/>
</bean>
test result 2:
To configure 3:
<!-- Use the specific name of the constructor's parameters to assign values -->
<bean id="user" class="com.hooi.pojo.User">
<constructor-arg name="userName" value="hooi3"/>
<constructor-arg name="gender" value="female"/>
</bean>
test result 3:
To configure 4:
<!-- Assignment by parameter type -->
<bean id="user" class="com.hooi.pojo.User">
<constructor-arg type="java.lang.String" value="hooi4"/>
<constructor-arg type="java.lang.String" value="female"/>
<constructor-arg type="int" value="18"/>
</bean>
test result 4: There are some small problems in the test , If User Class age The data type of is int, Don’t set… In the configuration file type Fill in the attribute with Interger type , There will be anomalies UnsatisfiedDependencyException.
These are the results of separate tests , If in userBean.xml All of the above are registered at the same time in user object , When getting context ,spring All the user created .
Test code :
package com.hooi.pojo;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class UserTest {
@Test
public void userTest(){
ApplicationContext context = new ClassPathXmlApplicationContext("userBean.xml");
System.out.println("-----------> Get context ");
User user = (User) context.getBean("user");
User user2 = (User) context.getBean("user2");
User user3 = (User) context.getBean("user3");
User user4 = (User) context.getBean("user4");
System.out.println("-----------> Get objects :"+user);
System.out.println("-----------> Get objects :"+user2);
System.out.println("-----------> Get objects :"+user3);
System.out.println("-----------> Get objects :"+user4);
}
}
test result :
above , We simply tested it through the constructor 【 There are parametric structures (3 Ways of planting ) And nonparametric construction 】 How to do dependency injection . Let’s continue to explore other ways of Injection .
setter Inject : The essence is in xml Use… In the document property attribute , Through parameterless constructors and set Method For injection .setter Injection requires the injected property , Must have set Method , And set The name of the method must be canonical (set+ Property name ), In the test , All lowercase property names do not report errors , however , For the sake of code normalization , Readability and aesthetics , It is better to use uppercase for the first letter of attribute name .
p Namespace Injection : In essence, it uses setter Inject , But it simplifies the amount of code , One line of code can do
c Namespace Injection It’s essentially a constructor injection .
In the test of constructor Injection , We only tested the injection of basic data types , The following types of test references . For testing purposes , Let’s recreate an entity class .
package com.hooi.pojo;
import java.util.*;
public class Student {
private String name;// full name
private int age;// Age
private Address address;// Address
private String[] books;// Favorite books
private List<String> hobbies;// hobby
private Set<String> skills;// specialty
private String motto;// motto
private Map<String,String> otherInfo;// Other information
public Student() {
}
public Student(String name, int age, Address address, String[] books, List<String> hobbies, Set<String> skills, String motto, Map<String, String> otherInfo) {
this.name = name;
this.age = age;
this.address = address;
this.books = books;
this.hobbies = hobbies;
this.skills = skills;
this.motto = motto;
this.otherInfo = otherInfo;
}
public void setName(String name) {
this.name = name;
}
public Student(String name, int age, Address address) {
this.name = name;
this.age = age;
this.address = address;
}
public void setAge(int age) {
this.age = age;
}
public void setAddress(Address address) {
this.address = address;
}
public void setBooks(String[] books) {
this.books = books;
}
public void setHobbies(List<String> hobbies) {
this.hobbies = hobbies;
}
public void setSkills(Set<String> skills) {
this.skills = skills;
}
public void setMotto(String motto) {
this.motto = motto;
}
public void setOtherInfo(Map<String, String> otherInfo) {
this.otherInfo = otherInfo;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", address=" + address +
", books=" + Arrays.toString(books) +
", hobbies=" + hobbies +
", skills=" + skills +
", motto='" + motto + '\'' +
", otherInfo=" + otherInfo +
'}';
}
}
Spring The configuration file
<?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"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--1. Constructor Injection -->
<!--1.1 There are parametric structures -->
<bean id="address1" class="com.hooi.pojo.Address">
<constructor-arg name="address" value=" Chang'an campus of Xi'an University of Posts and telecommunications, Xi'an City, Shaanxi Province "/>
</bean>
<!--1.2 No arguments structure -->
<bean id="address2" class="com.hooi.pojo.Address">
<property name="address" value=" Yanta campus of Xi'an University of Posts and telecommunications, Xi'an City, Shaanxi Province "/>
</bean>
<!--2. setter Inject -->
<bean id="student" class="com.hooi.pojo.Student">
<!--2.1 Basic type injection -->
<property name="name" value="Hooi"/>
<property name="age" value="18"/>
<!--2.2 Bean Type injection -->
<property name="address" ref="address1"/>
<!--2.3 Array type injection -->
<property name="books">
<array>
<value> The little prince </value>
<value> The world lost, </value>
<value> The temple of the Golden Pavilion </value>
<value> White night line </value>
</array>
</property>
<!--2.4 list Type injection -->
<property name="hobbies">
<list>
<value> stroke a cat </value>
<value> Walk the dog </value>
<value> Play the game </value>
</list>
</property>
<!--2.5 set Type injection -->
<property name="skills">
<set>
<value>Java Programming </value>
<value> Document writing </value>
</set>
</property>
<!--2.6 null Inject -->
<property name="motto">
<null/>
</property>
<!--2.7 Map Type injection -->
<property name="otherInfo">
<map>
<entry key=" Email address " value="[email protected]"/>
<entry key=" contact number " value="18888888888"/>
</map>
</property>
</bean>
<!--3. P Space naming Need to import constraints xmlns:p="http://www.springframework.org/schema/p"-->
<bean id="student2" class="com.hooi.pojo.Student" p:name="Hooi2" p:address-ref="address1"/>
<!--4. C Space naming Need to import constraints xmlns:c="http://www.springframework.org/schema/c"-->
<bean id="student3" class="com.hooi.pojo.Student" c:_0="Hooi3" c:_1="3" c:_2-ref="address2"/>
</beans>
Test code :
package com.hooi.pojo;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class StudentTest {
@Test
public void test1(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Student student = (Student) context.getBean("student");
Student student2 = (Student) context.getBean("student2");
Student student3 = (Student) context.getBean("student3");
System.out.println(student);
System.out.println(student2);
System.out.println(student3);
}
}
test result : In the test c When using the namespace injection method , The corresponding injection must be made according to the input parameters of the parameter constructor , The number of injected attributes should be consistent with the number of parameters required by the corresponding parametric construction method .
03 Annotation based injection
In addition to the above injection methods ,Spring It also provides a more convenient injection method — Annotation based injection . There are four annotations that can be used to register bean, Each annotation can be used at will , The effect is the same , But the semantics are different , To ensure the readability of the code , It is recommended to use it as follows :
- @Component: Can be used to register all bean
- @Repository: It is mainly used for registration dao Layer of bean
- @Service: It is mainly used for registration service Layer of bean
- @Controller: It is mainly used to register the control layer bean
Describe dependencies There are two main kinds of annotations : Namely @Autowired and @Resource
Spring 2.5 A note has been introduced since @Autowired, It can be used for class member variables 、 Method and constructor for annotation , Complete the work of automatic assembly . adopt @Autowired To eliminate set,get Method . however spring Of reference This approach is not advocated , Because this way it will Breaking the readability of module relationships .
@Autowired There are six properties of :
- No: Automatic assembly is not enabled . This is also Autowired The default value of , Reference relation shows declaration .
- byName: By the name of the attribute in IoC Find the corresponding in the container JavaBean Dependent objects and inject . The essence of this approach is to setter Inject . Such as ,UserServiceImpl Class has an attribute called userDao, So specify it Autowire The attribute is byName after ,Spring Of IoC The container looks in the configuration file for id/name The attribute is userDao Of bean, Then use the corresponding setter Inject it with .
- byType: Find by type of property JavaBean Dependent objects and inject . This is also Autowired Default The way . Same example , such as UserController Class has an attribute called userServiceImpl, The type is UserServiceImpl, that , Designate autowire The attribute is byType after ,Spring IoC The container will look for Class The attribute is UserServiceImpl Of bean, Use Seter Method to inject . If there are more than one of these types bean, That would be DefaultListableBeanFactory Of determineAutowireCandidate Method to determine which one to inject bean, This method will first find the victim @Primary Annotated bean, Look for the quilt @Order,@PriorityOrder Annotated bean, Finally, I will use byName The way to match . If in @Autowored() Under the use of @Qualifier (“{beanName}”) annotation , When more than one match the type bean when , Not invoke determineAutowireCandidate Method , It’s a direct search for @Qualifier designated beanName To find and inject , If it can’t be found, an exception will be thrown .@Qualifier And byName The difference is that byName Need to traverse , And the former will directly locate the corresponding bean, Therefore use @Qualifier Will be more efficient . example :
public class UserController {
@Autowired// Automatic assembly
@Qualifier("userServiceImpl")// Inject
private UserServiceImpl userServiceImpl;
}
-
constructor: Same as byType equally , It is also used to find dependent objects by type . And byType The difference is that it doesn’t use Seter Way into , Instead, it uses constructors to inject . If the container does not find the same type of constructor parameter bean, So throw an exception .
-
autodetect: stay byType and constructor Between the automatic choice of injection mode . adopt bean Class introspection mechanism (introspection) To decide whether to use constructor still byType Mode for automatic assembly . If you find the default constructor , So it’s going to use byType The way , Otherwise the constructor.
-
default: Labeled by a superior default-autowire Attribute determination . Be careful : In the configuration bean when , In the label Autowire Property takes precedence over its parent tag , That is , If defined in the parent tab default-autowire The attribute is byName, It is defined as byType when ,Spring IoC The container takes precedence over tag configuration .
<beans beans default-autowire="byType"
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="userDaoMysqlImpl" class="com.hooi.dao.Impl.UserDaoMysqlImpl"/>
<bean id="userServiceImpl" class="com.hooi.service.UserServiceImpl" p:userDao-ref="userDaoMysqlImpl"/>
<bean id="userController" class="com.hooi.controller.UserController" autowire="byName"/>
</beans>
@Resource yes Java Annotations , Default to byName To match the injection , If byType Find more than one match bean when , Need to use @Qualifier Annotation assignment bean The specific name of .
Type"
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="userDaoMysqlImpl" class="com.hooi.dao.Impl.UserDaoMysqlImpl"/>
<bean id="userServiceImpl" class="com.hooi.service.UserServiceImpl" p:userDao-ref="userDaoMysqlImpl"/>
<bean id="userController" class="com.hooi.controller.UserController" autowire="byName"/>
</beans>
@Resource yes Java Annotations , Default to byName To match the injection , If byType Find more than one match bean when , Need to use @Qualifier Annotation assignment bean The specific name of .