学习spring时候随手记录的一些笔记。
1. Spring配置文件格式如下
<?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">
<import resource="xx.xml"/>
<bean id="bean1" class="...">
<!-- collaborators and configuration for this bean go here -->
</bean>
<bean name="bean2" class="...">
<!-- collaborators and configuration for this bean go here -->
</bean>
<!-- more bean definitions go here -->
<alias alias="bean3" name="bean2"/> 别名
</beans>
Spring容器启动的基本条件:
- Spring的框架类包
- Bean配置信息
- Bean的实现类
Bean的元数据信息:
- Bean的实现类
- Bean的属性信息
- Bean的依赖关系
- Bean的行为配置
- Bean的创建方式
2 Spring Bean的命名:
用一个简单的例子说明:
首先,定义HelloWorld接口:
package com.company.springdemo2;
public interface HelloWorld {
public void sayHello();
}
定义HelloWorldImpl实现接口类:
package com.company.springdemo2;
public class HelloWorldImpl implements HelloWorld {
@Override
public void sayHello() {
System.out.println("hello world");
}
}
配置文件书写:
- 全限定类名,唯一: <bean class="com.company.springdemo2.HelloWorldImpl"/>
- 指定id,唯一:<bean id="helloWorld" class="com.company.springdemo2.HelloWorldImpl"/>
- 指定name,唯一:<bean name="hello" class="com.company.springdemo2.HelloWorldImpl"/>
- 同时指定id和name:<bean id="helloid" name="helloname" class="com.company.springdemo2.HelloWorldImpl"/> 在主函数中,id和name都能获取实例,这里不再说明。
- 指定多个name,第一个为标识符,其他为别名,唯一:用分隔符分开。
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 class="com.company.springdemo2.HelloWorldImpl"/>
<bean id="helloWorld" class="com.company.springdemo2.HelloWorldImpl"/>
<bean name="hello" class="com.company.springdemo2.HelloWorldImpl"/>
</beans>
Main方法:
package com.company.springdemo2;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
BeanFactory beanFactory = new ClassPathXmlApplicationContext("conf/helloworld.xml");
// //class
// HelloWorld helloWorld = beanFactory.getBean(HelloWorldImpl.class);
// helloWorld.sayHello();
// //id
// HelloWorld helloWorldID = beanFactory.getBean("helloWorld", HelloWorld.class);
// helloWorldID.sayHello();
//name
HelloWorld helloWorldName = beanFactory.getBean("hello",HelloWorld.class);
helloWorldName.sayHello();
}
}
3. Spring实例化Bean
SpringIOC根据Bean定义的配置元素数据使用反射机制创建Bean.
- 使用构造器实例化bean
- 使用静态工厂方法
- 使用实例工厂方法
3.1 构造器实例化: 构造器实例化是最简单的方式,Spring IOC容器既能使用默认空构造器也能使用有参数构造器两种方式创建Bean
用一个实例来说明;
首先,与上面相同,创建接口和实现类,这里,实现类有两个构造方法,有参数的和无参数的:
package com.company.springdemo3;
public interface HelloWorld {
public void sayHello();
}
package com.company.springdemo3;
public class HelloWorldImpl implements HelloWorld {
private String message;
/**
* 空构造器
* */
public HelloWorldImpl() {
this.message = "hello world";
}
/**
* 带参数的构造器
* */
public HelloWorldImpl(String message){
this.message = message;
}
@Override
public void sayHello() {
System.out.println(message);
}
}
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 id="helloworldNoArgs" class="com.company.springdemo3.HelloWorldImpl"/>
<!-- 有参数构造 -->
<bean id="helloworldWithArgs" class="com.company.springdemo3.HelloWorldImpl">
<!-- 指定参数 -->
<constructor-arg index="0" value="hello args"/>
</bean>
</beans>
Main
package com.company.springdemo3;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
BeanFactory beanFactory =
new ClassPathXmlApplicationContext("conf/hello2.xml");
// HelloWorld hello = beanFactory.getBean("helloworldNoArgs", HelloWorld.class);
// hello.sayHello();
HelloWorld hello = beanFactory.getBean("helloworldWithArgs", HelloWorld.class);
hello.sayHello();
}
}
3.2. 静态工厂实例化:使用静态工厂的方式除了指定必须的class属性,还要指定factory-method属性来指定实例化Bean的方法,而且使用静态工厂方法也允许指定方法参数,Spring Ioc容器将调用此属性来获取Bean
在3.1穿件的类的基础上,创建一个工厂类:
package com.company.springdemo3;
public class HelloWorldStaticFactory {
public static HelloWorld newInstance(String message){
return new HelloWorldImpl(message);
}
}
配置文件: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 id="helloworldStaticFactory"
class="com.company.springdemo3.HelloWorldStaticFactory"
factory-method="newInstance">
<constructor-arg index="0" value="hello static factory"/>
</bean>
</beans>
在Main函数中测试:
BeanFactory beanFactory =
new ClassPathXmlApplicationContext("conf/hello3.xml");
HelloWorld hello = beanFactory.getBean("helloworldStaticFactory", HelloWorld.class);
hello.sayHello();
3.3 实例工厂实例化:使用实例工厂方式不能指定class属性,此时必须使用factory-bean属性指定工厂bean,factory-method属性指定实例化bean的方法,而且使用实例工厂方法允许指定方法参数,方式和使用构造器方式一样:
添加实例工厂类:
package com.company.springdemo3;
public class HelloWorldInstanceFactory {
public HelloWorld newInstance(String message){
return new HelloWorldImpl(message);
}
}
添加配置文件.xml:
<!-- 1.定义实例工厂bean -->
<bean id="helloworldInstanceFactory"
class="com.company.springdemo3.HelloWorldInstanceFactory"/>
<!-- 2使用实例工厂Bean创建Bean -->
<bean id="helloworldInstance" factory-bean="helloworldInstanceFactory"
factory-method="newInstance">
<constructor-arg index="0" value="hello instance factory"></constructor-arg>
</bean>
在Main函数中测试:
BeanFactory beanFactory =
new ClassPathXmlApplicationContext("conf/hello3.xml");
HelloWorld hello = beanFactory.getBean("helloworldInstance", HelloWorld.class);
hello.sayHello();
4. Spring Bean的作用域:
Spring Bean中的作用域,在配置文件中即是“scope”, 在面向对象程序设计中一般指对象或变量的可见范围,而在spring容器中是指其创建的Bean对象相对于其他Bean对象的请求可见范围。
Bean的作用域类型与配置:在Spring容器中,一共提供了5中作用域类型,在配置文件中,通过scope来设置bean的作用域范围。
作用域 | 描述 |
---|---|
singleton | 该作用域将 bean 的定义的限制在每一个 Spring IoC 容器中的一个单一实例(默认)。<bean id="..." class="..." scope="singleton"/> |
prototype | 该作用域将单一 bean 的定义限制在任意数量的对象实例。<bean id="..." class="..." scope="singleton"/> |
request | 该作用域将 bean 的定义限制为 HTTP 请求。只在 web-aware Spring ApplicationContext 的上下文中有效。 |
session | 该作用域将 bean 的定义限制为 HTTP 会话。 只在web-aware Spring ApplicationContext的上下文中有效。 |
global-session | 该作用域将 bean 的定义限制为全局 HTTP 会话。只在 web-aware Spring ApplicationContext 的上下文中有效。 |
学习spring随手记下的一些笔记,具体可以参考:www.jikexueyuan.com