Spring framework learning notes Xml advanced configuration

Posted by todding01 on Tue, 04 Feb 2020 11:11:47 +0100

The second chapter has written the basic configuration method of XML, so this one starts to write the advanced configuration of XML (nothing, maybe I just said that...) Talk less now

1. Relationship between beans: inheritance or dependency

If you configure two beans, but find that many of the properties are the same, such as Car class, Car logo, origin, and price are not the same... At this time, you can think of a relationship that is inheritance, the child class inherits the parent class, and the child class has various properties in the parent class, and then rewrite different properties to complete the configuration of a new bean.

<bean id="adress" class="Person.Adress" p:city="BeiJing" p:street="WuDaokou"></bean>

<bean id="car" class="Person.Car" p:brand="Baoma" p:price="200000"></bean>

<bean id="adress2" parent="adress" p:street="TianAnmen"></bean>

<bean id="person" class="Person.Person" p:name="Tom" p:adress-ref="adress" p:car-ref="car"> </bean>

</beans>

The code does not take the car as an example, but rewrites an address class. Of course, it is the same. Add a parent attribute to the bean, and then rewrite different methods.

Dependency relationship

Association by depends on

<bean id="user6" parent="user" p:userName="Victoria"></bean>
<!-- Dependency relationship depents-on -->	
<bean id="user5" parent="user" p:userName="Backham" depends-on="user6"></bean>

2. Scope of bean

By default, Spring only creates a unique instance for each Bean declared in the IOC container, which can be shared across the scope of the IOC container: all subsequent getBean() calls and Bean references will return this unique Bean instance. This scope is called singleton, which is the default scope for all beans.

You can modify the scope through the scope in the bean.

 

3. Configuration of attribute file (important, easy to modify code)

Create a dp.properties property file to add the properties commonly used in configuration

jdbc.user=root
jdbc.password=123456
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql:///test
jdbc.initPoolSize=5
jdbc.maxPoolSize=10

Then import the external resource file in the bean

<context:property-placeholder location="classpath:db.properties"/>
	

Configuration bean (use ${property name} instead of the required value

<!-- Configure data sources -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="user" value="${jdbc.user}"></property>
		<property name="password" value="${jdbc.password}"></property>
		<property name="driverClass" value="${jdbc.driverClass}"></property>
		<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
		<property name="initialPoolSize" value="${jdbc.initPoolSize}"></property>
		<property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
</bean>

4.SpEL statement

Spring expression language (SpEL for short): it is a powerful expression language that supports runtime query and operation object graph.

SpEL provides convenience for dynamic assignment of bean properties, which can be realized through SpEL:

Reference the bean by its id

Call methods and properties in reference objects

Value of evaluation expression matching of regular expression

General:

Integer: < property name = "count" value = "{5}" / >
Decimal: < property name = "frequency" value = "{89.7}" / >
Scientific counting method: < property name = "capacity" value = "{1E4}" / >
String can be delimited by single or double quotation marks: < property name = "name" value = "{chuck '}" / > or < property name ='name' value = '{"Chuck"}' / >
Boolean: <property name="enabled" value="#{false}"/>

Operation symbols supported by spiel

 

The plus sign can also be used as a string connection:

 

Comparison operators: <, >, = =, < =, > =, lt, GT, EQ, Le, GE

 

If else operator:?: (Terry),?: (Elvis)

 

Logical operation symbols: and, or, not|

 

Regular expressions: matches

 

5. Automatic configuration

Not very tall, in fact, a simple group..

Automatic assembly: only beans are declared, and the relationship between beans is left to the IOC container
byType: automatic assembly is performed according to the type. However, it is required that there is only one bean corresponding to the type in the IOC container. If there are multiple beans, automatic assembly cannot be completed
byName: if the property name is consistent with the id name of a bean, automatic assembly can be completed. If there is no consistent id, automatic assembly cannot be completed

In the case of XML configuration, there are not many auto assemblies, but in the case of annotation based configuration, there are many auto assemblies

Write three classes first

package com.atguigu.spring.annotation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

@Controller
public class UserAction {
	
	@Autowired
	private UsreService usreService;
	
	public void execute(){
		System.out.println("Acceptance request");
		usreService.addNew();
	}
	
}
package com.atguigu.spring.annotation;

import org.springframework.stereotype.Service;

@Service
public class UserDao {
	
	public void save(){
		System.out.println("Save new user");
	}
	
}
package com.atguigu.spring.annotation;

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

@Service
public class UsreService {
	
	@Autowired
	private UserDao userDao;
	
	public void addNew(){
		System.out.println("Add new user");
		userDao.save();
	}
	
}

configuration file

<bean id="dao" class="com.atguigu.spring.ref.Dao">
	<property name="dataSource" value="C3P0"></property>				
	</bean>
<bean id="dao2" class="com.atguigu.spring.ref.Dao" scope="prototype"></bean>
	
<bean id="service" class="com.atguigu.spring.ref.Service" autowire="byName"></bean>
	
<bean id="action" class="com.atguigu.spring.ref.Action" autowire="byType"></bean>

 

Published 18 original articles, won praise 14, visited 6968
Private letter follow

Topics: JDBC Spring xml Attribute