IOC Assembly Bean (Note Mode)

1.Spring's Annotation Assembly Bean

First configure the file (applicationContext.xml) to introduce constraints

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""

Spring 2.5 introduces the use of annotations to define beans @Components to describe beans in the Spring framework

** Problem

Beginning my JDK1.8 can't recognize how to annotate!!Change to JDK1.7 to be recognized!!

public class Person{
    //Internal Code

//Equivalent to an xml file
<bean id="person" class="..."></bean>

Three annotations are provided in Spring's framework that are equivalent to the @Component annotation:

@Repository is used to label DAO implementation classes

@Service is used to label the Service implementation class

@Controller is used to label the ontroller implementation class

2. Bean property injection

1. General Attributes

@Value(value="Chen Wenlin")
private String infoString;

2. Object Properties

private UserDao userDao;

//Equivalent to
private UserDao userDao;

//Equivalent to
<property id="userDao" ref="userDao"></bean>

3. Configuration of other Bean properties

Configure Bean initialization and destruction methods:

* init-method and destroy-method.

@PostConstruct Initialization

@PreDestroy Destroy

Scope of configuring beans:


@Scope(value="prototype")   // Multiple cases
public class UserService {	
	@Value(value="Chen Wenlin")
	private String infoString;
	private UserDao userDao;
	public void init(){
		System.out.println("Initialization Method...");
	public void destory(){

1.6 Spring Integrated Junit Test

  1. There is a Junit environment in the program.
  2. Import a jar package
  3. @RunWith(SpringJUnit4ClassRunner.class)
    public class SpringTest {
    	private UserService userService;
    	public void demo1(){


Tags: Spring xml Junit encoding

Posted on Sat, 18 Jan 2020 08:50:45 -0800 by myaion