前面聊了spring中配置XML注入bean,但是现在进行一些补充:
创建bean的方式
其实前面配置的xml中bean,但是对于对象bean其有两种方式:
-
普通bean:配置文件中定义bean,然后得到的类型也就是配置的bean。 -
工厂bean:配置文件的定义的bean,但是可以于返回的类型不一样。 其实这个是通过实现接口FactoryBean的类作为工厂bean,然后通过接口里的方法得到方法中定义返回的bean类型。
这个说起来有些模糊,还是用代码演示的话会更加方便理解。
普通bean
这个前面演示的都是这种,所以就简单呈现一下:
<?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 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接口,然后看一下其需要实现的方法。
看一下具体是什么:
现在说一下这个几个方法:
- T getObject() : 这个其实是工厂模式,所以这个地方定义返回的bean对象。其实泛型所以可以定义返回的值。
- Class<?> getObjectType :这个是返回的对象是什么类型。
- boolean isSingleton : 默认是true,是单例模式,如果是False就是工厂模式。
现在演示一下:
<?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 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;
}
}
这个到时候需要聊一下单例模式和多例模式,这个后面具体聊。
补充bean的Scope
Scope 表示bean的作用域,javaweb中我们也聊过这个作用域,当然这个也可以对应上,比如:
Request :这个对象放在请求域中
Session :这个对象放在会话域中
global session :这个对象放在全局会话域中
不过说实话,前面三个在某些特定的场景中使用。
其实更加常用的是另外两个参数,而这两个才是需要重点聊。
参数值 | 模式 | 补充 |
---|
Sigleton | 单例模式 | 如果这样配置,是在加载spring配置文件的时候,就会创建单例实例对象。 | Prototype | 多例模式 | 如果这样配置,其创建对象是在调用(getBean)创建对象。 |
可以看出两个模式的优缺点,但是具体如何使用呢?
将这个类回归原始:
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设置单例模式
<?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 http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="beanfactory" class="com.xzd.test.TestFactoryBean" scope="singleton"></bean>
</beans>
scope设置多例模式
<?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 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 | 根据类型自动进行注入 |
现在进行演示,提取定义类:
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 );
}
}
通过名字自动注册:
<?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 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,要与要注入对象的属性名一样。
通过类型自动注册
<?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 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的话就不适合通过类型自动注册了。
注入外部属性文件
其实如果简单说这个概念的话,有些不好说,还是老规矩通例子进行演示:
这个最常用的应该配置jdbc,所以使用druid进行配置来演示一下。首先导入druid的jar包,这个就不再演示了。因为说实话不是为了演示连接池,而也不会调用数据,所以意义不大。
通spring配置的xml
<?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 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
<?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.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配置的至少需要知道一些。
|