spring5入门开发案例
Spring5 框架概述
1、Spring 是轻量级的开源的 JavaEE 框架
2、Spring 可以解决企业应用开发的复杂性
3、Spring 有两个核心部分:IOC 和 Aop
(1)IOC:控制反转,把创建对象过程交给 Spring 进行管理
(2)Aop:面向切面,不修改源代码进行功能增强
4、Spring 特点
(1)方便解耦,简化开发
(2)Aop 编程支持
(3)方便程序测试
(4)方便和其他框架进行整合
(5)方便进行事务操作
(6)降低 API 开发难度
概念解析
IOC
控制反转——Spring通过一种称作控制反转(IoC)的技术促进了低耦合。当应用了IoC,一个对象依赖的其它对象会通过被动的方式传递进来,而不是这个对象自己创建或者查找依赖对象。( 来源:百度百科 )
这谁呢听懂?
将对象的创造统一放到一个由工厂模式实现Bean容器中,
实现这一操作可以通过.xml文件或者注解的方式来实现。
底层对象的生产统一交给容器来管理,程序员只需要按照规定的格式在
xml文件或者类注解中配置即可。
当一个类需要 调用另一个类时,或者说依赖,调用类对象只需要找到
被调用类对象在容器中的唯一标识(id)即可以得到该类的对象。
从而省去了new 的操作。
代码实现
package com.itheima.dao;
/**
* @version 1.0
* @Author kevin
* @create 2022/7/13
*/
public interface UserDao {
public void save();
}
package com.itheima.dao;
/**
* @version 1.0
* @Author kevin
* @create 2022/7/13
*/
public class UserDaoImp implements UserDao{
public void save() {
System.out.println("UserDaoImp Method save...");
}
}
package com.itheima.dao;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* @version 1.0
* @Author kevin
* @create 2022/7/13
* 演示IOC的集合注入 其中
*/
public class UserDaoImp1 implements UserDao {
private List<String> stringList;
private String[] strings;
private Map<String, String> map;
private Set<String> set;
private Map<Integer,String>integerMap;
public void setIntegerMap(Map<Integer, String> integerMap) {
this.integerMap = integerMap;
}
public void setStringList(List<String> stringList) {
this.stringList = stringList;
}
public void setStrings(String[] strings) {
this.strings = strings;
}
public void setMap(Map<String, String> map) {
this.map = map;
}
public void setSet(Set<String> set) {
this.set = set;
}
public void save() {
System.out.println("List<String> " + stringList);
System.out.println("String [] " + Arrays.toString(strings));
System.out.println("Map<String,String> " + map);
System.out.println("Set<String> " + set);
System.out.println("integerMap "+integerMap);
}
}
package com.itheima.service;
/**
* @version 1.0
* @Author kevin
* @create 2022/7/13
*/
public interface UserService {
public void serviceSave();
}
package com.itheima.service;
import com.itheima.dao.UserDao;
/**
* @version 1.0
* @Author kevin
* @create 2022/7/13
*
* 演示set方法注入属性
*/
public class UserServiceImp implements UserService {
private UserDao userDao;
private String name;
public void setName(String name) {
this.name = name;
}
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public void serviceSave() {
System.out.println("UserServiceImp serviceSave...");
userDao.save();
System.out.println("name is"+name);
}
}
package com.itheima.service;
import com.itheima.dao.UserDao;
/**
* @version 1.0
* @Author kevin
* @create 2022/7/13
* 演示构造器方法注入属性
*/
public class UserServiceImp1 implements UserService {
private UserDao userDao;
private UserService userService;
private String name;
public UserServiceImp1(UserDao userDao, UserService userService, String name) {
this.userDao = userDao;
this.userService = userService;
this.name = name;
}
public void serviceSave() {
userDao.save();
userService.serviceSave();
System.out.println("name is " + name);
}
}
<?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">
<!-- 1,导入 spring-context 坐标,版本为5.2.10 RELEASE -->
<!-- bean的创建,本质是调用了类的无参构造器创建实例-->
<bean id="UserDaoImp" class="com.itheima.dao.UserDaoImp" ></bean>
<!-- 属性注入-->
<!-- 1,set方法注入-->
<bean id="UserServiceImp" class="com.itheima.service.UserServiceImp">
<property name="userDao" ref="UserDaoImp"></property>
<property name="name" value="kevin"></property>
</bean>
<!-- 2,构造器方法注入-->
<bean id="UserServiceImp1" class="com.itheima.service.UserServiceImp1">
<constructor-arg index="0" ref="UserDaoImp"/>
<constructor-arg index="1" ref="UserServiceImp"/>
<constructor-arg index="2" value="Miller"/>
</bean>
<!-- 集合注入-->
</beans>
<?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:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd">
<!-- 使用 util 标签完成 list 集合注入提取-->
<util:set id="setList">
<value>set1</value>
<value>set2</value>
<value>set3</value>
</util:set>
<bean id="UserDaoImp1" class="com.itheima.dao.UserDaoImp1">
<property name="set" ref="setList">
</property>
<property name="map">
<map>
<entry key="mKey1" value="mValue1"></entry>
<entry key="mKey2" value="mValue2"></entry>
</map>
</property>
<property name="stringList">
<array>
<value>list1</value>
<value>list2</value>
<value>list3</value>
</array>
</property>
<property name="strings">
<array>
<value>str1</value>
<value>str2</value>
</array>
</property>
<property name="integerMap">
<map>
<entry key="10" value="v1"></entry>
<entry key="11" value="v2"></entry>
</map>
</property>
</bean>
</beans>
import com.itheima.dao.UserDaoImp;
import com.itheima.dao.UserDaoImp1;
import com.itheima.service.UserServiceImp;
import com.itheima.service.UserServiceImp1;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @version 1.0
* @Author kevin
* @create 2022/7/13
*/
public class TestDemo {
@Test
public void text01(){
// 1,set方法注入
ApplicationContext context =
new ClassPathXmlApplicationContext("bean1.xml");
// UserDaoImp userDaoImp = (UserDaoImp)context.getBean("UserDaoImp");
// //UserDaoImp userDaoImp1 = (UserDaoImp)context.getBean("UserDaoImp");
//
// System.out.println(userDaoImp);
//// System.out.println(userDaoImp1);
// userDaoImp.save();
UserServiceImp userServiceImp = (UserServiceImp) context.getBean("UserServiceImp");
userServiceImp.serviceSave();
}
@Test
public void text02(){
// 2,构造器方法注入
ApplicationContext context =
new ClassPathXmlApplicationContext("bean1.xml");
UserServiceImp1 userServiceImp1 = (UserServiceImp1) context.getBean("UserServiceImp1");
userServiceImp1.serviceSave();
}
@Test
public void test03(){
//IOC的集合类注入
ApplicationContext context = new ClassPathXmlApplicationContext("bean2.xml");
UserDaoImp1 userDaoImp1 = (UserDaoImp1) context.getBean("UserDaoImp1");
userDaoImp1.save();
}
}
初来乍到,今天就先copy到这里。。 下次从bean的生命周期开始C..