目录
3、编写spring核心配置文件 applicationContext.xml (应用程序上下文配置文件)
1.applicationContext.xml中标签的配置
今日资料下载: 直接下载spring01.zip 网盘备份下载
一、搭建简单spring框架
1、新建项目,导入jar包
2、编写service层接口与实现类
UserService.java
package cn.ahpu.service;
public interface UserService {
public void sayHello();
}
UserServiceImpl.java
package cn.ahpu.service;
public class UserServiceImpl implements UserService {
@Override
public void sayHello() {
System.out.println("Hello Spring!!");
}
}
3、编写spring核心配置文件 applicationContext.xml (应用程序上下文配置文件)
applicationContext.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标签配置需要由spring创建并管理的类 -->
<bean id="userService" class="cn.ahpu.service.UserServiceImpl"></bean>
</beans>
4、编写测试程序
Demo1.java
package cn.ahpu.Test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.ahpu.service.UserService;
import cn.ahpu.service.UserServiceImpl;
/**
* 测试IOC的程序 以后会由action调用service
* @author 软件163韩竹安
* 2019年12月25日-下午1:19:54
*/
public class Demo1 {
//原来方式
@Test
public void run1(){
UserService us=new UserServiceImpl();
us.sayHello();
}
//新方式要写配置文件
//spring框架的方式
@Test
public void run2(){
//创建工厂,加载核心配置文件 (一加载工厂就会帮你创建配置了的对象)
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");//classPath表示加载src路径下的文件
//从工厂获取对象 ac.getBean("id值")
UserService usi = (UserService) ac.getBean("userService");
//调用对象方法执行
usi.sayHello();
}
}
二、详细学习
1.applicationContext.xml中标签的配置
bean标签详解
1. id属性和name属性的区别 (id不可出现特殊字符 name可以出现特殊字符)
* id -- Bean起个名字,在约束中采用ID的约束,唯一
* 取值要求:必须以字母开始,可以使用字母、数字、连字符、下划线、句话、冒号 id:不能出现特殊字符
* name -- Bean起个名字,没有采用ID的约束(了解)
* 取值要求:name:出现特殊字符.如果<bean>没有id的话 , name可以当做id使用
* Spring框架在整合Struts1的框架的时候,Struts1的框架的访问路径是以/开头的,例如:/bookAction
2. class属性 -- Bean对象的全路径
3. scope属性 -- scope属性代表Bean的作用范围
* singleton -- 单例(默认值)
* prototype -- 多例,在Spring框架整合Struts2框架的时候,Action类也需要交给Spring做管理,配置把Action类配置成多例!!
* request -- 应用在Web项目中,每次HTTP请求都会创建一个新的Bean(少用)
* session -- 应用在Web项目中,同一个HTTP Session 共享一个Bean(少用)
* globalsession -- 应用在Web项目中,多服务器间的session
4. Bean对象的创建和销毁的两个属性配置(了解)
* 说明:Spring初始化bean或销毁bean时,有时需要作一些处理工作,因此spring可以在创建和拆卸bean的时候调用bean的两个生命周期方法
* init-method -- 当bean被载入到容器的时候调用init-method属性指定的方法
* destroy-method -- 当bean从容器中删除的时候调用destroy-method属性指定的方法
* 想查看destroy-method的效果,有如下条件
* scope= singleton有效
* web容器中会自动调用,但是main函数或测试用例需要手动调用(需要使用ClassPathXmlApplicationContext的close()方法)
小结:
1.id不可出现特殊字符,name可以出现特殊字符
没有id时,name可以当id用
2.scope:singleton默认值单例
prototype 多例 Action必须配置为多例
(每个请求一个单独action,那么每个请求就都有一个单独的值栈,取值存值安全多了)
2.依赖注入DI
service需要用到dao,但是所有的类都不能自己new呀,那么就要在service里写个dao类的属性,然后配置bean是多配置个property属性,依赖注入把该属性的值传入,传入一个dao给service
a.简单演示注入String类型的属性
serviceImpl中加一个属性
配置文件中多注入属性值
测试方法直接拿值
2.service中依赖注入一个dao
UserDaoImpl.java
package cn.ahpu.dao;
public class UserDaoImpl implements UserDao {
@Override
public void save() {
System.out.println("我是dao层save");
}
}
UserServiceImpl.java
package cn.ahpu.service;
import cn.ahpu.dao.UserDao;
import cn.ahpu.dao.UserDaoImpl;
public class UserServiceImpl implements UserService {
//依赖注入方式
private UserDaoImpl userDao;
public void setUserDao(UserDaoImpl userDao) {
this.userDao = userDao;
}
@Override
public void save() {
System.out.println("我是service层save");
//new UserDaoImpl().save();
userDao.save();
}
}
applicationContext.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标签配置需要由spring创建并管理的类 -->
<bean id="userService" class="cn.ahpu.service.UserServiceImpl" scope="prototype">
<property name="userDao" ref="userDao"/><!-- ref那个bean的id值 -->
</bean>
<!-- spring一启动就会创建一个userDao类 -->
<bean id="userDao" class="cn.ahpu.dao.UserDaoImpl"/>
</beans>
Demo1.java
package cn.ahpu.Test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.ahpu.service.UserService;
import cn.ahpu.service.UserServiceImpl;
/**
* 测试IOC的程序 以后会由action调用service
* @author 软件163韩竹安
* 2019年12月25日-下午1:19:54
*/
public class Demo1 {
//依赖注入方式
@Test
public void run5(){
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");//classPath表示加载src路径下的文件
UserServiceImpl usi = (UserServiceImpl) ac.getBean("userService");
usi.save();
}
}
其他注入方式(了解)
构造方法注入:
package com.itheima.demo4;
public class Car1 {
private String name;
private Double price;
//public Cart() {}//注意没有空参构造方法 配置时必须提供参数
public Car1(String name, Double price) {
super();
this.name = name;
this.price = price;
}
@Override
public String toString() {
return "Car1 [name=" + name + ", price=" + price + "]";
}
}
<bean id="car1" class="com.itheima.demo4.Car1">
<!-- 配置构造方法的参数 那么框架创建此类时就拿着参数调用对应的构造方法 -->
<!-- <constructor-arg name="name" value="奇瑞QQ"/>
<constructor-arg name="price" value="35000"/> -->
<!-- 另一种配置参数的方式 index="0"表示第一个构造参数 ="1"表示第二个构造参数-->
<constructor-arg index="0" value="奇瑞QQ2"/>
<constructor-arg index="1" value="50000"/>
</bean>
/**
* 通过构造方法实现注入 用得不多 标准用法是set方法
*/
@Test
public void run1(){
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
Car1 car1=(Car1) ac.getBean("car1");
System.out.println(car1);
}
今日代码汇总:
UserDao.java
package cn.ahpu.dao;
public interface UserDao {
public void save();
}
UserDaoImpl.java
package cn.ahpu.dao;
public class UserDaoImpl implements UserDao {
@Override
public void save() {
System.out.println("我是dao层save");
}
}
UserService.java
package cn.ahpu.service;
public interface UserService {
public void sayHello();
public void save();
}
UserServiceImpl.java
package cn.ahpu.service;
import cn.ahpu.dao.UserDao;
import cn.ahpu.dao.UserDaoImpl;
public class UserServiceImpl implements UserService {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public void sayHello() {
System.out.println("Hello Spring!!");
}
//依赖注入方式
private UserDaoImpl userDao;
public void setUserDao(UserDaoImpl userDao) {
this.userDao = userDao;
}
@Override
public void save() {
System.out.println("我是service层save");
//new UserDaoImpl().save();
userDao.save();
}
}
applicationContext.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标签配置需要由spring创建并管理的类 -->
<bean id="userService" class="cn.ahpu.service.UserServiceImpl" scope="prototype">
<property name="name" value="天河"/>
<property name="userDao" ref="userDao"/><!-- ref那个bean的id值 -->
</bean>
<!-- spring一启动就会创建一个userDao类 -->
<bean id="userDao" class="cn.ahpu.dao.UserDaoImpl"/>
</beans>
Demo1.java
package cn.ahpu.Test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.ahpu.service.UserService;
import cn.ahpu.service.UserServiceImpl;
/**
* 测试IOC的程序 以后会由action调用service
* @author 软件163韩竹安
* 2019年12月25日-下午1:19:54
*/
public class Demo1 {
//原来方式
@Test
public void run1(){
UserService us=new UserServiceImpl();
us.sayHello();
}
//新方式要写配置文件
//spring框架的方式
@Test
public void run2(){
//创建工厂,加载核心配置文件 (一加载工厂就会帮你创建配置了的对象)
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");//classPath表示加载src路径下的文件
//从工厂获取对象 ac.getBean("id值")
UserService usi = (UserService) ac.getBean("userService");
//调用对象方法执行
usi.sayHello();
}
//依赖注入传入属性值
@Test
public void run3(){
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");//classPath表示加载src路径下的文件
UserServiceImpl usi = (UserServiceImpl) ac.getBean("userService");
System.out.println(usi.getName());
}
//依赖注入:在service里注入dao
/*//原始方式 serviceimpl的save里直接new
@Test
public void run4(){
UserServiceImpl usi = new UserServiceImpl();
usi.save();
}*/
//依赖注入方式
@Test
public void run5(){
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");//classPath表示加载src路径下的文件
UserServiceImpl usi = (UserServiceImpl) ac.getBean("userService");
usi.save();
}
}