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="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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">
   <context:annotation-config></context:annotation-config>
</beans>

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!!

@Component("person")
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

@Autowired
@Qualifier(value="userDao")
private UserDao userDao;

//Equivalent to
@Resource(name="userDao")
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

@Service("userService")
@Scope(value="prototype")   // Multiple cases
public class UserService {	
	
	@Value(value="Chen Wenlin")
	private String infoString;
	
	@Autowired
	@Qualifier(value="userDao")
	private UserDao userDao;
	
	@PostConstruct
	public void init(){
		System.out.println("Initialization Method...");
	}
	
	@PreDestroy
	public void destory(){
		System.out.println("destroy-method...");
	}

1.6 Spring Integrated Junit Test

  1. There is a Junit environment in the program.
  2. Import a jar package
  3. @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations="classpath:applicationContext.xml")
    public class SpringTest {
    	@Autowired
    	private UserService userService;
    	
    	@Test
    	public void demo1(){
    		userService.sayHello();
    	}
    }

     

Tags: Spring xml Junit encoding

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