Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the assembly method of Bean in Spring

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "what is the assembly method of Bean in Spring". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "what is the assembly method of Bean in Spring"?

Preface

The assembly of Bean can be understood as dependency injection, and the assembly mode of Bean is the way of Bean dependency injection. Spring container supports many forms of Bean assembly, such as XML-based assembly, Annotation-based assembly and automatic assembly (the most common of which is annotation-based assembly). This article will mainly explain the use of three assembly methods.

XML-based assembly mode 1: setting injection

Condition: the ① Bean class must have a no-parameter constructor; the ② Bean class must provide a setter method for the property.

In the configuration file, use elements to inject values for each attribute

Method 2: structural injection

Condition: the Bean class must provide a parametric constructor

In the configuration file, use elements to inject values for parameters

The specific implementation steps are as follows

Create the Java class User, which provides parametric and nonparametric constructors and attribute setter methods

Package com.ssm.assemble;import java.util.List;public class User {private String username; private Integer password; private List list; / * 1. Use Construction injection * 1.1 to provide a parametric construction method with all parameters. * / public User (String username, Integer password, List list) {super (); this.username = username; this.password = password; this.list = list;} / * * 2. The default empty parameter constructor is provided using set value injection * 2.1, and the setter method is provided for all properties. * / public User () {super ();} public void setUsername (String username) {this.username = username;} public void setPassword (Integer password) {this.password = password;} public void setList (List list) {this.list = list @ Override public String toString () {return "User [username=" + username + ", password=" + password + ", list=" + list + "]";}}

Create the Spring profile beans1.xml, and assemble the Bean using the above two methods

"constructorvalue1"constructorvalue2"setlistvalue1"setlistvalue2"

Create the test class XmlBeanAssembleTest, test the program

Package com.ssm.assemble;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class XmlBeanAssembleTest {public static void main (String [] args) {/ / define the profile path String xmlPath = "beans1.xml"; / / load the configuration file ApplicationContext applicationContext = new ClassPathXmlApplicationContext (xmlPath) / / output result System.out.println (applicationContext.getBean ("user1")) in construction mode; / / output result System.out.println (applicationContext.getBean ("user2")) in setting value mode;}}

Running effect.

Annotation-based assembly

Because the XML-based assembly may cause the XML configuration file to be too bloated, it will bring some difficulties to the follow-up maintenance and upgrade. Therefore, Spring provides full support for Annotation (annotations) technology.

The specific implementation steps are as follows

Create a data access (Dao) layer interface UserDao

Package com.ssm.annotation;public interface UserDao {public void save ();}

Create an implementation class UserDaoImpl for the data access (UserDao) layer interface, and add the corresponding annotation @ Repository ("userDao") before the implementation class.

Package com.ssm.annotation;import org.springframework.stereotype.Repository;@Repository ("userDao") / / @ Repository ("userDao") is equivalent to: public class UserDaoImpl implements UserDao {@ Override public void save () {System.out.println ("userdao...save...");}} in the configuration file

Create business (Service) layer interface UserService

Package com.ssm.annotation;public interface UserService {public void save ();}

Create the implementation class UserServiceImpl of the Service interface and add the setter method of the attribute object UserDao, and add the corresponding annotation @ Service ("userService") before the implementation class and @ Resource (name= "userDao") before the method in the class.

Package com.ssm.annotation;import javax.annotation.Resource;import org.springframework.stereotype.Service;@Service ("userService") / / equivalent to: public class UserServiceImpl implements UserService {@ Resource (name= "userDao") / / equivalent to: private UserDao userDao; public void save () {/ / call the save method this.userDao.save () in userDao System.out.println ("userservice....save...");} public void setUserDao (UserDao userDao) {this.userDao = userDao;}}

Create a control (Controller) layer Java class UserController, and add the corresponding annotation @ Controller ("userController") before implementing the class and @ Resource (name= "userService") before the method in the class.

Package com.ssm.annotation;import javax.annotation.Resource;import org.springframework.stereotype.Controller;@Controller ("userController") / / equivalent to: public class UserController {@ Resource (name= "userService") / / equivalent to: private UserService userService; public void save () {this.userService.save (); System.out.println ("userController...save...") in the configuration file } public void setUserService (UserService userService) {this.userService = userService;}}

Create Spring configuration file beans2.xml, open annotations and define Bean

Create the test class AnnotationAssembleTest, test the program

Package com.ssm.annotation;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class AnnotationAssembleTest {public static void main (String [] args) {/ / define configuration file path String xmlPath = "beans2.xml"; / / load configuration file ApplicationContext applicationContext = new ClassPathXmlApplicationContext (xmlPath); / / get UserController instance UserController userController = (UserController) applicationContext.getBean ("userController") / / call the save () method userController.save () in UserController;}}

Running effect.

Automatic assembly

The so-called automatic assembly is to automatically inject a Bean into the Property of other Bean. The element of Spring contains an autowire attribute, and we can automatically assemble Bean by setting the attribute value of autowire. The autowire property has five values, and their values and descriptions are shown in the following table:

The specific implementation steps are as follows

Here, you only need to change the annotation mode to automatic assembly in the annotation-based assembly configuration file (beans.xml). The specific code is as follows:

It works the same as an annotation-based assembly.

At this point, I believe you have a deeper understanding of "what is the assembly method of Bean in Spring". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report