Notes the development of [IOC] Spring of Spring

First, create a project, the introduction of the jar package:

Create a java project called spring, we have a way to copy log4j.properties file in the development Spring4, in addition to the introduction of basic development package, it also needs to introduce aop package. So we need the jar package as follows:
Here Insert Picture Description

Second, create a Spring configuration file

Create a src directory under the file "applicationContext.xml", and the introduction of dtd constrained context, because it is necessary for the development of annotations. In addition, we also need to configure the component scans in the configuration file, like those used to scan the packages under use @Component notes:

<?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-4.2.xsd">

	<context:component-scan base-package="cn.jingpengchong.vo"/>
</beans>

Third, create a Move interfaces and classes MoveWalk

Use annotation class will develop ways to manage and instantiate Spring, you need to add it @Component notes, the case is to get the Bean according to the Class instance of the class object class corresponding to default, we can value the annotation when the attribute value is assigned, so that the Bean can be obtained according to the value attribute:

package cn.jingpengchong.vo;

import org.springframework.stereotype.Component;

@Component("move")
//该注解相当于配置了:<bean name="move" class="cn.jingpengchong.vo.MoveWalk"/>
public class MoveWalk implements Move {
	@Override
	public void move() {
		System.out.println("步行。。。");
	}
}

Test Methods:

public static void main(String[] args) {
	ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
	Move move = (Move) context.getBean("move");
	move.move();
}

Results are as follows:
Here Insert Picture Description

Fourth, for the class of common property assignment: @Value ()

If you want spring which is common property assignment, we can add @Value set method on the class attribute in the corresponding class is instantiated when () annotations in parentheses can write attribute value, if it is not set method, the Notes add effects on the property is the same. For example, the transformation of the above MoveWalk categories:

package cn.jingpengchong.vo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component("move")
public class MoveWalk implements Move {

	@Value("张三")
	private String name;
	@Override
	public void move() {
		System.out.println(name + "步行。。。");
	}
}

Run the test class results are as follows:
Here Insert Picture Description

Fifth, the class notes

Annotations @Component for modifying a class, the class to instantiate and manage Spring. The annotation function derived from the same three notes, although the same function, but the timing of the application have different recommendations:

  • @Controller : This annotation is recommended for use in web layer;
  • @Service : This annotation is recommended in the service layer;
  • The @Repository : This annotation is recommended in the dao layer.

Sixth, property notes

@Value : The annotation of the set values for common attributes;
@Autowired : annotation is used to set the attributes of object types, but it is in accordance with the type of containers to match the IOC object assignment. If you want to assign by name, the comment will need to be used in conjunction with @Qualifier annotation, annotation @Qualifier value of attribute assignment, Spring will be in accordance to the attribute value matching objects IOC container;
@Resource : This annotation can @Autowired achieve @Qualifier and joint use of injection-implemented properties by name.

Seven other notes

@PostConstruct : annotation initialization method, init-method corresponding to property tag bean
@PreDestroy : annotation destruction methods, corresponding to destroy-method attribute tag bean
@Scope : annotation scope equivalent to the scope attribute bean tag, the same value

Published 128 original articles · won praise 17 · views 2721

Guess you like

Origin blog.csdn.net/qq_43705275/article/details/104484084
Recommended