Six methods of java framework spring dependency injection

Source: Internet
Author: User
Tags oracle database

In spring, how does one copy the attributes of an object?

1) constructor
2) inject values to attributes using the set method
3) p namespace
4) automatic configuration transfer (learn more, not recommended)
5) annotation
6) interface

Preparation (simulated business method) Action --> service --> dao

1) UserDao:

P <span style = "font-family: Courier New;"> ublic class UserDao {public void save () {System. out. println ("save method") ;}</span>


2) UserService:

P <span style = "font-family: Courier New;"> ublic class UserService {private UserDao userDao; public void setUserDao (UserDao userDao) {this. userDao = userDao;} public void save () {userDao. save () ;}}</span>


3) UserAction:
 

<Span style = "font-family: Courier New;"> public class UserAction {private UserService userService; public void setUserService (UserService userService) {this. userService = userService;} public String execute () {userService. save (); return null ;}}</span>


4) App Test class:

Public class App {ApplicationContext ac = new ClassPathXmlApplicationContext ("cn/itcast/property/bean. xml "); @ Test public void test () {UserAction userAction = (UserAction) ac. getBean ("userAction"); System.out.println(userAction.exe cute ());}}


5) bean. xml constraints

<? 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: 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"> </beans>


Method 1. set method injection

1) the set method must be ensured.

<Bean id = "userDao" class = "cn. itcast. property. userDao "> </bean> <bean id =" userService "class =" cn. itcast. property. userService "> <property name =" userdao "ref =" userDao "> </property> </bean> <bean id =" userAction "class =" cn. itcast. property. userAction "> <property name =" userService "ref =" userService "> </property> </bean>


Method 2. Internal bean (Because it is an internal bean, if there is another action, the service method in it cannot be referenced)

Bean id = "userAction" class = "cn. itcast. property. userAction "> <property name =" userService "> <bean id =" userService "class =" cn. itcast. property. userService "> <property name =" userDao "> <bean id =" userDao "class =" cn. itcast. property. userDao "> </bean> </property> </bean>


Method 3. p namespace

<Bean id = "userDao" class = "cn. itcast. property. userDao "> </bean> <bean id =" userService "class =" cn. itcast. property. userService "p: userdao-ref =" userDao "> </bean> <bean id =" userAction "class =" cn. itcast. property. userAction "p: userService-ref =" userService "> </bean>


Method 4: automatic assembly (not recommended)

<! -- Automatically assemble according to "name": attributes injected by userAction will be automatically searched for in ioc container with the same name as the attributes -->

<Bean id = "userDao" class = "cn. itcast. property. userDao "> </bean> <bean id =" userService "class =" cn. itcast. property. userService "autowire =" byName "> </bean> <bean id =" userAction "class =" cn. itcast. property. userAction "autowire =" byName "> </bean>


Method 5: Annotation

Steps for using annotations:

1) first introduce the context namespace

Xmlns: context = "http://www.springframework.org/schema/context"

2) enable annotation scanning

<Context: component-scanbase-package = "cn. itcast. e_anno2"> </context: component-scan>

3) Use annotations

Add the object to the ioc container by annotation.

Create objects and process object dependencies. Related annotations:

@ Component specifies to add an object to the IOC container

@ Repository: same as @ Component; used in the persistence layer

@ Service acts the same as @ Component; used at the business logic layer

@ Controller works the same as @ Component; used at the control layer

@ Resource property injection


Enable scanning in the configuration file

<! -- Enable scan mechanism --> <context: component-scan base-package = "cn. itcast. property"> </context: component-scan>


UserDao uses annotations (the set method is not required if annotations are used)

@ Repository public class UserDao {public void save () {System. out. println ("save method ");}}


UserService usage annotation:

@ Service public class UserService {@ Resource private UserDao userDao; public void save () {userDao. save ();}}


UserAction usage annotation

@ Controller public class UserAction {@ Resource private UserService userService; public String execute () {userService. save (); return null ;}}


Method: interface injection)


Interface injection refers to defining the information to be injected in the interface and completing the injection through the interface. The procedure is as follows.
(1) compile an interface, IBusiness, and inject various databases through this interface. The sample code of IBusiness. java is as follows:
// ******** IBusiness. java **************
}
(2) any class that wants to use the database instance must implement this interface. The Business logic class implements this interface IBusiness. The example code of Business. java is as follows:
// ******** Business. java **************
This. db = db;
}
......
// Obtain data from the database ××× based on the injected database class
Public void getData (){
......
Db. getData ();
......
}
}
(3) compile the test class TestBusiness. The sample code of TestBusiness. java is as follows:
// ******** TestBusiness. java **************
Public class TestBusiness {
Private Business business = new Business ();
......
// Obtain data from the Oracle database based on the injected database class
Public void getData (){
......
Business. createDI (new OracleDataBase ());
Business. getData ();
......
}
}
If you want to inject dependency objects, you must implement the IBusiness interface.

Related Article

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.