Spring快速入门
<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"> beans>
<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="userService" class="com.cyh.service.IUserServiceImpl">bean>beans>
package com.cyh.test;
import com.cyh.service.IUserService;
import com.cyh.service.IUserServiceImpl;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Lesson02 {
//现在使用UserService方式从spring容器中获取
@Test
public void test2(){
//1.加载beans.xml这个spring配置文件
ApplicationContext context =
new ClassPathXmlApplicationContext("com/cyh/beans.xml");
//2.从spring容器(context)获取IUserService对象
IUserService userService = (IUserServiceImpl)context.getBean("userService");
//3.调用
userService.add();
}
}
//注意:通过getBean得到的对象,无论创建几个,都是一个对象!
IoC Inverse of Control反转控制的概念,就是将原本在程序中手动创建UserService对象的控制权,交由Spring框架管理,简单说,就是创建UserService对象的控制权被反转到了Spring框架。
Dependency Injection依赖注入,在Spring框架负责创建Bean对象时,动态的将依赖对象注入到Bean组件。
在UserService中提供一个get/set的name方法,在beans.xml中通过property去注入
在Userservice中提供get/set的name方法
package com.cyh.service;
public class IUserServiceImpl implements IUserService{
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public void add() {
System.out.println("创建用户......"+name);
}
}
在beans.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 id="userService" class="com.cyh.service.IUserServiceImpl">
<property name="name" value="caiyuhao">property>
bean>
beans>
第一种:ClassPathXmlApplicationContext
ApplicationContext context =
new ClassPathXmlApplicationContext("com/cyh/beans.xml");
IUserService userService = (IUserServiceImpl)context.getBean("userService");
第二种:文件系统路径获取跑配置文件
//1.加载beans.xml这个spring配置文件
ApplicationContext context =
new FileSystemXmlApplicationContext( "F:\\Idea_Project\\spring\\demo3\\src\\com\\cyh\\beans.xml");
//2.从spring容器(context)获取IUserService对象
IUserService userService = (IUserServiceImpl)context.getBean("userService");
第三种:使用BeanFactory(了解)
String path="F:\\Idea_Project\\spring\\demo3\\src\\com\\cyh\\beans.xml";
BeanFactory factory = new XmlBeanFactory(new FileSystemResource(path));
//2.从spring容器(context)获取IUserService对象
IUserService userService = (IUserServiceImpl)factory.getBean("userService");
spring内部创建对象的原理:
1.解析xml文件,获取类名,id,属性
2.通过反射,用类型创建对象
3.给创建的对象赋值
注:spring的配置文件通常放在src目录下
第一种:new实现类
<bean id="userService1" class="com.cyh.service.IUserServiceImpl">bean>
第二种:通过静态工厂方法
<bean id="userService2" class="com.cyh.service.IUserServiceFactory1" factory-method="createUserService">bean>
注意:使用此工厂方法需要JDK的版本为:1.7。
public class IUserServiceFactory1 {
public static IUserService createUserService(){
return new IUserServiceImpl();
}
}
第三种:通过实例工厂方法
public class IUserServiceFactory {
public IUserService createUserService(){
return new IUserServiceImpl();
}
}
<bean id="factory" class="com.cyh.service.IUserServiceFactory">bean>
<bean id="userService3" factory-bean="factory" factorymethod="createUserService">bean>
@Test
public void test5(){
ApplicationContext context =
new ClassPathXmlApplicationContext("com/cyh/bean3.xml");
IUserService userService2 = (IUserService)context.getBean("userService3");
userService2.add();
}
仅需要掌握前两个
类别 | 说明 |
---|---|
singleton | 在Spring IoC容器中仅存在一个Bean实例,Bean以单例方式存在,默认值。 |
prototype | 每次从容器中调用Bean时,都返回一个新的实例,即每次调用getBean()时,相当于执行new XxxBean()。(可以理解成多例) |
request | 每次HTTP请求都会创建一个新的Bean,该作用域仅适用于WebApplicationContext环境。 |
session | 同一个HTTP Session共享一个Bean,不同Session使用不同Bean,仅适用于WebApplicationContext环境。 |
globalSession | 一般用于Portlet应用环境,该作用域仅适用于WebApplicationContext环境。 |
<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="userService" class="com.cyh.service.IUserServiceImpl" scope="singleton">bean>
<bean id="userService" class="com.cyh.service.IUserServiceImpl" scope="prototype">bean>
beans>
举例演示
创建一个基类User.java
package com.cyh.model;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.*;
public class User implements BeanNameAware , BeanFactoryAware , InitializingBean , DisposableBean {
private String username;
private String password;
public User() {
System.out.println("1.实例化");
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
System.out.println("2.赋值属性:"+username);
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User{" +
"username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
@Override
public void setBeanName(String s) {
System.out.println("3.设置Bean的名字:"+s);
}
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
System.out.println("4.Bean工厂:"+beanFactory);
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("6.属性赋值完成...");
}
public void myInit(){
System.out.println("7.自定义初始化方法...");
}
@Override
public void destroy() throws Exception {
System.out.println("9.Bean对象被销毁...");
}
public void myDestroy(){
System.out.println("10.自定义的销毁方法...");
}
}
新建beans.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 id="user" class="com.cyh.model.User" init-method="myInit" destroy-method="myDestroy">
<property name="username" value="one">property>
<property name="password" value="two">property>
bean>
<bean id="beanProcess" class="com.cyh.model.MyBeanPostProcessor">bean>
beans>
新建MyBeanPostProcessor.java
package com.cyh.model;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
public class MyBeanPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object o, String s) throws BeansException {
//这边可以用于多个对象共同的事情处理
System.out.println("5.预处理:"+o+":"+s);
return o;
}
@Override
public Object postProcessAfterInitialization(Object o, String s) throws BeansException {
System.out.println("8.后处理:"+o+":"+s);
return o;
}
}
新建测试类测试Bean生命周期执行流程
package com.cyh.test;
import com.cyh.model.User;
import com.cyh.service.IUserService;
import com.cyh.service.IUserServiceImpl;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import javax.jws.soap.SOAPBinding;
import java.lang.reflect.InvocationTargetException;
public class Lesson05 {
@Test
public void test1() {
ApplicationContext context = new ClassPathXmlApplicationContext("com/cyh/beans5.xml");
User user = (User)context.getBean("user");
System.out.println(user);
try {
context.getClass().getMethod("close").invoke(context);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
}
构造方法注入
<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="stu" class="com.cyh.model.Student">
<constructor-arg name="username" value="cyh">constructor-arg>
<constructor-arg name="age" value="23">constructor-arg>
bean>
beans>
通过索引加类型 给构造方法赋值
<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="stu" class="com.cyh.model.Student">
<constructor-arg index="0" value="cyh" type="java.lang.String"> constructor-arg>
<constructor-arg index="1" value="23" type="int">constructor-arg>
bean>
beans>
通过set方法往bean注入属性值
<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="stu" class="com.cyh.model.Student">
<property name="username" value="cyh">property>
<property name="password" value="123">property>
<property name="age" value="23">property>
bean>
beans>
通过p命名空间注入(了解)
需要在beans.xml文件中引入p标签的支持
xmlns:p="http://www.springframework.org/schema/p"
使用方法
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="stu" class="com.cyh.model.Student"
<property name="address" ref="address">property>
<property name="address" value="#{address}">property>
bean>
beans>
集合注入都是给添加子标签
演示
创建模型类Programmer.java
package com.cyh.model;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public class Programmer {
private List<String> cars;
private Set<String> pats;
private Map<String,Integer> people;
private Properties tx;
private String[] array;
public String[] getArray() {
return array;
}
public void setArray(String[] array) {
this.array = array;
}
public Properties getTx() {
return tx;
}
public void setTx(Properties tx) {
this.tx = tx;
}
public Map<String, Integer> getPeople() {
return people;
}
public void setPeople(Map<String, Integer> people) {
this.people = people;
}
public List<String> getCars() {
return cars;
}
public void setCars(List<String> cars) {
this.cars = cars;
}
public Set<String> getPats() {
return pats;
}
public void setPats(Set<String> pats) {
this.pats = pats;
}
@Override
public String toString() {
return "Programmer{" +
"cars=" + cars +
'}';
}
}
创建beans.xml文件并配置数据
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="pro" class="com.cyh.model.Programmer">
<property name="cars">
<list>
<value>丰田value>
<value>宝马value>
<value>大众value>
list>
property>
<property name="pats">
<set>
<value>猫value>
<value>狗value>
set>
property>
<property name="people">
<map>
<entry key="one" value="10">entry>
<entry key="two" value="20">entry>
map>
property>
<property name="tx">
<props>
<prop key="url">mysql:jdbc://localhost:3306/dbprop>
<prop key="user">rootprop>
<prop key="password">123456prop>
props>
property>
<property name="array">
<array>
<value>123value>
<value>456value>
<value>789value>
array>
property>
bean>
beans>
在测试类中测试即可。
注:web开发中,提供3个@Component注解衍生注解(功能一样)取代
语法 | 解释说明 |
---|---|
@Repository(“名称”) | dao层 |
@Service(“名称”) | service层 |
@Controller(“名称”) | web层 |
@Autowired | 自动根据类型注入 |
@Qualifier(“名称”) | 指定自动注入的id名称 |
@Resource(“名称”) | |
@PostConstruct | 自定义初始化 |
@PreDestroy | 自定义销毁 |
注意:默认情况下,spring是不开启注解功能的,需要在beans.xml文件中加上下面两行:
xmlns:context="http://www.springframework.org/schema/context"
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"
beans.xml全部配置如下:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
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:annotation-config/>
<context:component-scan base-package="com.cyh"/>
beans>
在IUserServiceImpl.java中添加注解@Component
package com.cyh.service;
import com.cyh.model.User;
import org.springframework.stereotype.Component;
//@Component 没有添加id则测试类中需要通过类名来获取实例对象
@Component("userService") //指定id名称的话测试类中可以通过id来获取实例对象
public class IUserServiceImpl implements IUserService{
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public void add() {
System.out.println("创建用户......"+name);
}
@Override
public void add(User user) {
System.out.println("添加用户"+user);
}
}
测试类代码如下:
package com.cyh.test;
import com.cyh.model.User;
import com.cyh.service.IUserService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Lesson10 {
@Test
public void test1() {
ApplicationContext context = new ClassPathXmlApplicationContext("com/cyh/beans10.xml");
//如果@Component没配置id,则通过类型获取
//IUserService bean = (IUserService)context.getBean(IUserServiceImpl.class);
//如果@Component配置id,则通过id获取
IUserService bean = (IUserService)context.getBean("userService");
User user = new User();
user.setUsername("zhangsan");
bean.add(user);
}
}