Detailed introduction to Spring

Preface This article is followed by the detailed introduction to spring (1). It is recommended to read the first article before reading this article. ...
Preface

This article is followed by the detailed introduction to spring (1). It is recommended to read the first article before reading this article. The links are as follows:

Detailed introduction to Spring (1) https://www.cnblogs.com/jichi/p/10165538.html

I. spring injection mode

1. set method injection

<bean name="user" > <property name="name" value="Xiao Ming"></property> <property name="age" value="18"></property> </bean>

2. Construction method injection

<bean name="user" > <constructor-arg name="name" value="Xiaohong" ></constructor-arg> <constructor-arg name="age" value="50"></constructor-arg> </bean>

3. p namespace injection

xmlns:p="http://www.springframework.org/schema/p"
<bean name="user" p:name="Xiao Bai" p:age="10"></bean>

4. spel expression injection

<bean name="user"> <property name="name" value="Xiaohong"></property> <property name="age" value="18"></property> </bean> <bean name="user1"> <property name="name" value="#"></property> <property name="age" value="#"></property> </bean>
II. spring complex type injection
public class Collection { public String[] arr; public List<String> list; public Map<String,Object> map; public Properties props; public String[] getArr() { return arr; } public void setArr(String[] arr) { this.arr = arr; } public List<String> getList() { return list; } public void setList(List<String> list) { this.list = list; } public Map<String, Object> getMap() { return map; } public void setMap(Map<String, Object> map) { this.map = map; } public Properties getProps() { return props; } public void setProps(Properties props) { this.props = props; } @Override public String toString() { return "Collection [arr=" + Arrays.toString(arr) + ", list=" + list + ", map=" + map + ", props=" + props + "]"; } }

1. Array type injection

<bean name="collect"> <property name="arr"> <array> <value>xiaohei</value> <value>xiaobai</value> </array> </property> </bean>

2. list type injection

<bean name="collect"> <property name="list"> <list> <value>xiaohei</value> <value>xiaobai</value> </list> </property> </bean>

3. map type injection

<bean name="collect"> <property name="map"> <map> <entry key="name" value="xiaohei"></entry> <entry key="age" value="18"></entry> </map> </property> </bean>

4. properties type injection

<bean name="collect"> <property name="props"> <props> <prop key="name">xiaohei</prop> <prop key="age">18</prop> </props> </property> </bean>
3. Configure spring to start initialization with web project

Configure in web.xml.

<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param>
IV. sub configuration files of spring

Method 1:

ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext1.xml","applicationContext2.xml")

Mode two:

<import resource="applicationContext.xml"></import>
V. spring annotation configuration

1. Enable annotation scanning

<context:component-scan base-package="com.jichi.entity"></context:component-scan>

Scan for annotations in all classes under com.jichi.entity.

2. Add notes to the class

@Component public class User { }
6. spring common notes

1, @ composet, @ Controller, @ Service, @ Repository are four component annotations that act on the class. There is no difference between the four notes, just for convenience.

2. The @ Scope annotation works on the class.

@Scope(scopeName="singleton") //Singleton mode public class User { }
@Scope(scopeName="prototype") //manyExample mode public class User { }

3, @ Value is used to inject common type Value

The first way is to break the encapsulation of objects by reflecting the value of filed on attributes.

@Value("xiaohei") private String name;

The second way is to assign a value through the set method without destroying the encapsulation of the object.

@Value("xiaobai") public void setName(String name) { this.name = name; }

4 @ Autowired, @ Resource, @ Qualifier comments

Please refer to the previous blog for details on the assembly method of reference type.

@Autowired private Car car;
@Resource private Car car;

5, @ PostConstruct and @ PreDestroy

@PostConstruct //Call before creating object public void init(){ System.out.println("initial"); } @PreDestroy //Call before object destruction public void destory(){ System.out.println("Destruction"); }

2 December 2019, 02:41 | Views: 1326

Add new comment

For adding a comment, please log in
or create account

0 comments