前面聊了spring中配置XML注入bean,但是现在进行一些补充:
创建bean的方式
其实前面配置的xml中bean,但是对于对象bean其有两种方式:
- 普通bean:配置文件中定义bean,然后得到的类型也就是配置的bean。
- 工厂bean:配置文件的定义的bean,但是可以于返回的类型不一样。 其实这个是通过实现接口FactoryBean的类作为工厂bean,然后通过接口里的方法得到方法中定义返回的bean类型。
[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:98274615-6b30-49eb-b819-3b7d4313e8d4
[En]
[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:588cd89e-ca08-4b0b-b262-c445210c53d7
普通bean
[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:c5c45545-6e63-4cd8-9391-362dffa99ce0
[En]
[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:1629234a-f63a-4ea4-a906-dce63548f13f
<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 http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="person" class="com.xzd.test.Person">bean>
beans>
public class Person {
}
public class test {
public static void main(String[] args) {
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("person.xml");
Person person1=applicationContext.getBean("person",Person.class);
Person person2=applicationContext.getBean("person",Person.class);
System.out.println(person1);
System.out.println(person2);
System.out.println(person1==person2);
}
}
这里可以看出普通创建bean其创建的是一个对象,其像是单例模式了,用的是同一个对象。然后你运行多次,这个对象也不变。
工厂bean
既然是实现FactoryBean接口,然后看一下其需要实现的方法。
看一下具体是什么:
[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:acce6511-267a-4b39-a4d8-533c79302034
[En]
[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:a487c2e5-23ab-4e0b-b73f-0dfe60770dc5
- T getObject() : 这个其实是工厂模式,所以这个地方定义返回的bean对象。其实泛型所以可以定义返回的值。
- Class getObjectType :这个是返回的对象是什么类型。
- boolean isSingleton : 默认是true,是单例模式,如果是False就是工厂模式。
现在演示一下:
<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 http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="beanfactory" class="com.xzd.test.TestFactoryBean">bean>
beans>
package com.xzd.test;
public class Person {
String personname;
public void setPersonname(String personname) {
this.personname = personname;
}
@Override
public String toString() {
return "person={name:"+this.personname+"}";
}
}
ackage com.xzd.test;
import org.springframework.beans.factory.FactoryBean;
public class TestFactoryBean implements FactoryBean<Person> {
@Override
public Person getObject() throws Exception {
Person person=new Person();
person.setPersonname("张三");
return person;
}
@Override
public Class<Person> getObjectType() {
return Person.class;
}
@Override
public boolean isSingleton() {
return FactoryBean.super.isSingleton();
}
}
public class test {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beanfactory.xml");
Person person1 = applicationContext.getBean("beanfactory", Person.class);
Person person2 = applicationContext.getBean("beanfactory", Person.class);
System.out.println("person1:----"+person1);
System.out.println("person2:----"+person2);
System.out.println(person1==person2);
}
}
其实可以看出是单例模式,如果修改TestFactoryBean如下:
ackage com.xzd.test;
import org.springframework.beans.factory.FactoryBean;
public class TestFactoryBean implements FactoryBean<Person> {
@Override
public Person getObject() throws Exception {
Person person=new Person();
person.setPersonname("张三");
return person;
}
@Override
public Class<Person> getObjectType() {
return Person.class;
}
@Override
public boolean isSingleton() {
return false;
}
}
[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:b3f556a2-e7a1-4d54-a59d-41c189814e14
[En]
[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:3155707e-f214-4572-ab57-82397b0fb001
补充bean的Scope
Scope 表示bean的作用域,javaweb中我们也聊过这个作用域,当然这个也可以对应上,比如:
Request :这个对象放在请求域中
Session :这个对象放在会话域中
global session :这个对象放在全局会话域中
[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:af3b6fc8-7526-4cb7-96ba-256ceefc42d5
[En]
[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:3279bfc9-48ae-4ba5-9a40-cac3070e6e6b
[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:27420722-5332-48c7-b5fc-63837566068b
[En]
[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:c665efd7-78b1-4ce7-9daa-f74ebda7dd74
参数值模式补充Sigleton单例模式如果这样配置,是在加载spring配置文件的时候,就会创建单例实例对象。Prototype多例模式如果这样配置,其创建对象是在调用(getBean)创建对象。
- 单例模式: 没有必要创建多个同样的实例,减少内存资源的消耗。 但是这里涉及一个线程安全性的问题,如果bean实例是有状态的,那么多线程环境下可能会出现问题。
- 多例模式: 每次请求都产生一个bean, 这样多线程操作时,如果bean是有状态的, 线程之间是安全的
[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:2402bc17-f242-4e40-b66e-69e374ed81e3
[En]
[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:2b1506f8-f537-4b26-b647-ab3d3d62f449
将这个类回归原始:
ackage com.xzd.test;
import org.springframework.beans.factory.FactoryBean;
public class TestFactoryBean implements FactoryBean<Person> {
@Override
public Person getObject() throws Exception {
Person person=new Person();
person.setPersonname("张三");
return person;
}
@Override
public Class<Person> getObjectType() {
return Person.class;
}
@Override
public boolean isSingleton() {
return FactoryBean.super.isSingleton();
}
}
然后调用的类:
public class test {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beanfactory.xml");
Person person1 = applicationContext.getBean("beanfactory", Person.class);
Person person2 = applicationContext.getBean("beanfactory", Person.class);
System.out.println("person1:----"+person1);
System.out.println("person2:----"+person2);
System.out.println(person1==person2);
}
}
package com.xzd.test;
public class Person {
String personname;
public void setPersonname(String personname) {
this.personname = personname;
}
@Override
public String toString() {
return "person={name:"+this.personname+"}";
}
}
上面三个java文件不会再修改,所以下面两个演示都用这三个java文件。
scope设置单例模式
<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 http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="beanfactory" class="com.xzd.test.TestFactoryBean" scope="singleton">bean>
beans>
scope设置多例模式
<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 http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="beanfactory" class="com.xzd.test.TestFactoryBean" scope="prototype">bean>
beans>
XML自动装配
先说定义:根据指定装配规则(属性名称活着属性类型),spring自动将匹配的属性值进行注入。
这个可以先演示之前用的注入方式其实时非自动,而这现在聊这个自动注解是bean属性中的autowrite。
而autowire有两个重要值:
属性值描述byname根据名字自动进行注入bytype根据类型自动进行注入
[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:c90204c9-cbaf-4f47-a029-835db26b2dc8
[En]
[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:177b76ac-9a29-42ef-955e-d8af74a51668
package com.xzd.test;
public class Student {
Person person;
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
@Override
public String toString() {
return "Student={name:"+this.person.name+"}";
}
}
package com.xzd.test;
public class Person {
String name;
public Person(String name) {
this.name = name;
}
}
public class test {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("student.xml");
Student student=applicationContext.getBean("student",Student.class);
System.out.println(student );
}
}
通过名字自动注册:
<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 http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="student" class="com.xzd.test.Student" autowire="byName">
bean>
<bean id="person" class="com.xzd.test.Person">
<constructor-arg name="name" value="张三">constructor-arg>
bean>
beans>
可以看出这个直接自动注册,不需要通过property标签进行手动注册。
这个需要注意一点事: 这个地方因为是通过名字注册配置bean的id,要与要注入对象的属性名一样。
通过类型自动注册
<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 http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="student" class="com.xzd.test.Student" autowire="byType">
bean>
<bean id="person" class="com.xzd.test.Person">
<constructor-arg name="name" value="李四">constructor-arg>
bean>
beans>
可以看出两种都是自动进行了注册,看似没有什么区别,但是需要注意一点,通过名字注册的话可以注册不同的通一个类型,但是如果注册多个相同类型bean的话就不适合通过类型自动注册了。
注入外部属性文件
[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:b305aef0-4599-47d9-b5a5-cc7a82b2b4d7
[En]
[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:ab4fc0f9-c9b8-4b0a-a0a1-1a8f79726e46
这个最常用的应该配置jdbc,所以使用druid进行配置来演示一下。首先导入druid的jar包,这个就不再演示了。因为说实话不是为了演示连接池,而也不会调用数据,所以意义不大。
通spring配置的xml
<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 http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean name="druidPoolTest" class="com.xzd.test.DruidPoolTest">
<property name="driverClassName" value="com.mysql.jdbc.Driver">property>
<property name="url" value="jdbc:mysql://localhost:3306/test?rewriteBatchedStatments=true">property>
<property name="username" value="root">property>
<property name="password" value="root">property>
bean>
beans>
package com.xzd.test;
public class DruidPoolTest {
String driverClassName;
String url;
String username;
String password;
public void setDriverClassName(String driverClassName) {
this.driverClassName = driverClassName;
}
public void setUrl(String url) {
this.url = url;
}
public void setUsername(String username) {
this.username = username;
}
public void setPassword(String password) {
this.password = password;
}
}
public class test {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("druidpool.xml");
DruidPoolTest druidPoolTest=applicationContext.getBean("druidPoolTest",DruidPoolTest.class);
System.out.println(druidPoolTest );
}
}
注入外部属性
因为重复,所以因为只修改了xml,所以就鞋xml
<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:property-placeholder location="druid.properties">context:property-placeholder>
<bean name="druidPoolTest" class="com.xzd.test.DruidPoolTest">
<property name="driverClassName" value="${driverClassName}">property>
<property name="url" value="${url}">property>
<property name="username" value="${username}">property>
<property name="password" value="${password}">property>
bean>
beans>
这里可以看出多了一个context标签,所以要看头部新增的配置信息,然后看如下结果。
不过说实话,虽然配置文件可以解决很多问题,但是开发过程中使用最多的是注释,但是xml配置的至少需要知道一些。
Original: https://blog.csdn.net/u011863822/article/details/127092557
Author: 小猪弟
Title: ssm框架之spring:xml配置再补充
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/561204/
转载文章受原作者版权保护。转载请注明原作者出处!