In the previous article, I learned how to inject simple values. Since there are many types that can be injected, let's try it out here.
For example, define two classes, User.java
and UserAddress.java
: define the following attributes
in the User.java
class:
private int age;
private String name;
private String[] hobbeys;
private List<String> cards;
private Map<String, String> girlfirends;
private Set<String> houses;
private UserAddress address;
Then generate the corresponding set
sum get
method. In UserAddress.java
the definition:
private String address;
Similar production set
, get
and toString
methods can be.
Then, start writing the corresponding beans.xml
documents, may wish to refer to the official document: https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#beans-collection-elements
for writing convenience, you can beans.xml
Right-click on the top of the file Split Vertically
, and the effect is as follows:
more convenient. Then you can start.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="weizu" class="com.weizu.pojo.User">
<property name="age" value="23"></property>
<property name="name" value="张三"></property>
<property name="hobbeys">
<array>
<value>抽烟</value>
<value>喝酒</value>
</array>
</property>
<property name="cards">
<list>
<value>123131231312</value>
<value>123131231312</value>
</list>
</property>
<property name="girlfirends">
<map>
<entry key="one" value="对象1"/>
<entry key="two" value="对象2"/>
</map>
</property>
<property name="houses">
<set>
<value>北京</value>
<value>上海</value>
</set>
</property>
<property name="address">
<null/>
</property>
</bean>
</beans>
test:
import com.weizu.pojo.User;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class myTest {
@Test
public void Test(){
// create and configure beans
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
User userService = (User) context.getBean("weizu");
System.out.println(userService.toString());
}
}
result:
Reference video: https://www.bilibili.com/video/BV1WE411d7Dv?p=9&spm_id_from=pageDriver
Reference document: https://docs.spring.io/spring-framework/docs/current/reference/html/core.html #beans-collection-elements
When there are multiple different beans-xxx.xml
files, you can define a total XML
, and then use the import
import in it, such as:
<beans>
<import resource="services.xml"/>
<import resource="resources/messageSource.xml"/>
<import resource="/resources/themeSource.xml"/>
<bean id="bean1" class="..."/>
<bean id="bean2" class="..."/>
</beans>
services.xml
Must be in a position classpath location
, while the two rear position must be resources
under, these paths are relative to the path. Of course, this path can be specified as an absolute path, such as: file:C:/config/services.xml
or classpath:/config/services.xml
.
In Spring IoC
the vessel, you can host multiple bean
, at configuration time, some of the data may be configured as follows:
class
Qualification, the designatedbean
category;name
The specifiedbean
instantiation name;scope
Specifiedbean
range of action, there are optionalsingleton
, ,prototype
,request
,session
,application
,websocket
by defaultsingleton
, each represents aSpring IoC
single container, the presence ofbean
the object instance;prototype
represents abean
possible promising multiple instances;request
represents a singleHTTP
single request instance;session
Similarly also to aHTTP
ofSession
Single instance;application
meansServletContext
single instancewebsocket
inWebSocket
medium ; means single instance in medium;
The specific designation method is as follows:
<bean id="accountService" class="com.something.DefaultAccountService" scope="singleton"/>