Bean assembly method of Spring framework

In the Spring framework, beans can be assembled in a variety of ways, including:

  1. XML configuration file: Use the <bean> tag to define the configuration information of the Bean, and then refer to these beans in the XML configuration file.
  2. Annotations: Use annotations on the Bean class to identify it as a Bean, and use other annotations to define the properties and behavior of the Bean.
  3. Java configuration class: Use a Java class to configure a bean, usually a @Configuration class, and use the @Bean annotation to define a bean.

        These assembly methods can use singleton or prototype scope, and can also use constructor or setter methods to inject dependencies. Which assembly method to choose depends on the developer's personal preference and the needs of the project. Below I will only talk about the XML configuration method and annotation configuration.

1. Use the XML configuration file to assemble the Bean:

The directory is as follows 

 

First, we create a UserDao interface in the Dao layer,

package com.spring.dao;

public interface UserDao {
	void getName();
}

Then write two classes to implement the methods inside.

UserDaoImpl class

package com.spring.dao;

public class UserDaoImpl implements UserDao{
	public void getName() {
		System.out.println("获取所有用户数据!");
	}
}

UserMysqlImpl class

package com.spring.dao;

public class UserMysqlImpl implements UserDao{
	public void getName() {
		System.out.println("获取所有Mysql用户的数据!");
	}
}

Then create an interface UserService in the business layer

package com.spring.service;

import com.spring.dao.UserDao;

public interface UserService {
	void getName();
	void setUserDao(UserDao userdao);
}

Then create a UserServiceImpl class to implement the method. The setUserDao method implements dependency injection, that is, when we need to use the object, use the setUserDao method to inject the UserDao object into the current userdao attribute

package com.spring.service;

import com.spring.dao.UserDao;

public class UserServiceImpl implements UserService{
	UserDao userdao;
	public void setUserDao(UserDao userdao) {
		this.userdao=userdao;
	}
	public void getName() {
		userdao.getName();
	}
}

         Then we look at the most important ApplicationContext.xml configuration file (the above code can be copied directly without explanation), and use <bean> and <property> tags for IOC container configuration.

比如<bean id="UserServiceImpl" class="com.spring.service.UserServiceImpl">

<property name="UserDao" ref="UserMysqlImpl" />

</bean> means

        The id of the bean is "UserServiceImpl", and the corresponding class is "com.spring.service.UserServiceImpl". This bean refers to the previously defined bean named "UserMysqlImpl", which is injected into the "UserDao" property of the "UserServiceImpl" object through the <property> element.

<?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">
	<!-- 将指定类TestDaoImpl配置给Spring,让Spring创建其实例 -->
	<bean id="UserDaoImpl" class="com.spring.dao.UserDaoImpl" />
	<bean id="UserMysqlImpl" class="com.spring.dao.UserMysqlImpl" />
	<bean id="UserServiceImpl" class="com.spring.service.UserServiceImpl">
	<property name="UserDao" ref="UserMysqlImpl" />
	
	</bean>
</beans>

        In this way, when the Spring IOC container is initialized, it will automatically create and inject the required Bean objects, decoupling the business logic code from the specific implementation, and improving the scalability and maintainability of the code.

Then we write a test class. First create a Spring IoC container based on an XML configuration file, then get a Bean instance named "UserServiceImpl" from the IoC container, cast it to the UserServiceImpl type, and finally call the getName() method of the UserServiceImpl instance

package com.spring.servlet;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.spring.service.UserServiceImpl;

public class Test {
	public static void main(String args[]) {
		ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
		//容器在手,天下我有
		UserServiceImpl us = (UserServiceImpl)context.getBean("UserServiceImpl");
		us.getName();
	}
}

2. Use annotations for Bean assembly

        Ok, we have just completed the assembly of beans using xml files, and then we use annotations to assemble beans on this basis.

        First, let's look at the ApplicationContext.xml file. The first half of the content is very different from the previous one, so I won’t say anything about it in detail, it can be copied directly. Look below,

<?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"
	xmlns:aop="http://www.springframework.org/schema/aop"
	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
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">
        
	<context:component-scan base-package="com.spring.dao"/>
	<context:component-scan base-package="com.spring.service"/>

	
</beans>

        <context:component-scan base-package="com.spring.dao"/>This code is used to automatically scan the classes under the specified package, and inject the classes annotated with @Repository as Beans. Here it means to scan com.spring.daothe classes under the package, and @Repositoryautomatically inject the classes with annotations as beans.

        What does that mean? Let's look at the UserDaoImpl class of the Dao layer. He added a comment @Repository("userdao") in front of the class name, so it means injecting the UserDaoImpl class into the Bean. One annotation can solve the problem, so we don't have to use the <bean> tag as much trouble as before.

package com.spring.dao;

import org.springframework.stereotype.Repository;

@Repository("userdao")
public class UserDaoImpl implements UserDao{
	public void getName() {
		System.out.println("获取所有用户数据!");
	}
}

        Then we look at the changes in the business layer 

package com.spring.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.spring.dao.UserDao;
@Service("service")
public class UserServiceImpl implements UserService{
	@Autowired
	UserDao userdao;
	@Value("张三")
	public String name;
	public void getName() {
		userdao.getName();
	}
}

        @Service("service") means to register the UserServiceImpl class into the Spring container, where "service" is the id of the bean. (The class in the service layer should use @Service)

        The @Autowired annotation should be viewed together with @Repository("userdao"), and inject the object instantiated by the UserDaoImpl class into userdao (there is no setter method here, but @Autowired is used for attribute injection)

        @Value("Zhang San") indicates that the attribute value "Zhang San" is injected into the name member variable

And finally in the test class

package com.spring.servlet;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.spring.service.UserServiceImpl;

public class Test {
	public static void main(String args[]) {
		ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
		UserServiceImpl us = (UserServiceImpl)context.getBean("service");
		us.getName();
		System.out.println(us.name);
	}
}

Call the bean whose id is service and call the getName() method

The output is as follows

 

Guess you like

Origin blog.csdn.net/m0_56540237/article/details/129243742