Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert One-On-One J2EE Development and Design中阐述的部分理念和原型衍生而来。它是为了解决企业应用开发的复杂性而创建的。框架的主要优势之一就是其分层架构,分层架构允许使用者选择使用哪一个组件,同时为 J2EE 应用程序开发提供集成的框架。Spring使用基本的JavaBean来完成以前只可能由EJB完成的事情。然而,Spring的用途不仅限于服务器端的开发。从简单性、可测试性和松耦合的角度而言,任何Java应用都可以从Spring中受益。Spring的核心就是一个轻量级的控制反转(IoC)和面向切面(AOP)的框架!。
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
<!-- 单元测试jar包-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
UserDao接口
public interface UserDao {
public void getUser();
}
UserDao接口的多个实现类
public class UserDaoImpl implements UserDao {
public void getUser() {
System.out.println("获取默认数据!");
}
}
public class UserDaoMysqlImpl implements UserDao{
public void getUser() {
System.out.println("获取Mysql的数据!");
}
}
public class UserDaoOracleImpl implements UserDao {
public void getUser() {
System.out.println("获取Oracle的数据!");
}
}
public class UserDaoServerImpl implements UserDao {
public void getUser() {
System.out.println("获取Server的数据!");
}
}
UserService 接口
public interface UserService {
public void getUser();
}
UserServiceImpl实现类
public class UserServiceImpl implements UserService {
private UserDao userDao;
public void setUserDao(UserDao userDao){
this.userDao = userDao;
}
public void getUser() {
userDao.getUser();
}
}
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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!--
bean 配置需要创建的对象
id 对象名
class 实例的全限定类名
-->
<bean id="userService" class="com.li.service.UserServiceImpl"/>
</beans>
public class MyTest {
@Test
public void test() {
//获得spring容器
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//获得需要的对象
UserServiceImpl userService = context.getBean("userService", UserServiceImpl.class);
//需要使用那个类就创建
userService.setUserDao(new UserDaoMysqlImpl());
userService.getUser();
}
}
配置文件
<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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="user" class="com.li.pojo.User">
property name="name" value="小明"/>
bean>
<alias name="user" alias="newuser">alias>
<bean id="user" class="com.li.pojo.User" name="u2;u3 u4,u6">
<property name="name" value="小明"/>
bean>
beans>
public class User {
private String name;
public User(){
System.out.println("User的无参构造");
}
public User(String name){
this.name= name;
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public void show() {
System.out.println("name="+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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!--IOC创建对象的方式 当配置文件bean标签创建的时候 User已经被实例化了
-->
<!-- 使用无参构造创建对象 默认!-->
<bean id="user" class="com.li.pojo.User">
<property name="name" value="小明"/>
</bean>
<!-- 有参构造创建对象的三种方式-->
<!-- 方式一:通过索引赋值-->
<bean id="user" class="com.li.pojo.User">
<constructor-arg index="0" value="小红"/>
</bean>
<!-- 方式二:通过参数类型匹配 多个参数不推荐使用-->
<bean id="user" class="com.li.pojo.User">
<constructor-arg type="java.lang.String" value="开心"/>
</bean>
<!-- 方式三:通过name 进行赋值-->
<bean id="user" class="com.li.pojo.User" name="u5">
<constructor-arg name="name" value="欢喜"/>
</bean>
</bean>
public class Student {
private String name;
private Address address;
private String[] books;
private List<String> hobbys;
private Map<String,String> card;
private Set<String> games;
private String wife;
private Properties info;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public String[] getBooks() {
return books;
}
public void setBooks(String[] books) {
this.books = books;
}
public List<String> getHobbys() {
return hobbys;
}
public void setHobbys(List<String> hobbys) {
this.hobbys = hobbys;
}
public Map<String, String> getCard() {
return card;
}
public void setCard(Map<String, String> card) {
this.card = card;
}
public Set<String> getGames() {
return games;
}
public void setGames(Set<String> games) {
this.games = games;
}
public String getWife() {
return wife;
}
public void setWife(String wife) {
this.wife = wife;
}
public Properties getInfo() {
return info;
}
public void setInfo(Properties info) {
this.info = info;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", address=" + address.toString() +
", books=" + Arrays.toString(books) +
", hobbys=" + hobbys +
", card=" + card +
", games=" + games +
", wife='" + wife + '\'' +
", info=" + info +
'}';
}
}
//Address实体类
public class Address {
private String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Address{" +
"address='" + address + '\'' +
'}';
}
}
<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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="address" class="com.li.pojo.Address">
<property name="address" value="西安"/>
bean>
<bean name="student" class="com.li.pojo.Student">
<property name="name" value="李瑞"/>
<property name="address" ref="address"/>
<property name="books">
<array>
<value>英语四级value>
<value>英语六级value>
<value>计算机二级value>
array>
property>
<property name="hobbys">
<list>
<value>敲代码value>
<value>听音乐value>
<value>打篮球value>
list>
property>
<property name="card">
<map>
<entry key="身份证" value="789456123456454658"/>
<entry key="银行卡" value="1321312314564454"/>
map>
property>
<property name="games">
<set>
<value>和平精英value>
<value>王者荣耀value>
<value>球球作战value>
set>
property>
<property name="wife">
<null/>
property>
<property name="info">
<props>
<prop key="学号">20183306prop>
<prop key="宿舍号">12号楼226prop>
props>
property>
bean>
beans>
Student{name='李瑞',
address=Address{address='西安'},
books=[英语四级, 英语六级, 计算机二级],
hobbys=[敲代码, 听音乐, 打篮球],
card={身份证=789456123456454658,
银行卡=1321312314564454},
games=[和平精英, 王者荣耀, 球球作战],
wife='null',
info={学号=20183306, 宿舍号=12号楼226}}
package com.li.pojo;
/**
* @Author: Lenovo
* @CreateTime: 2020-05-27 17:48
* @Description:西部开源教育科技有限公司
*/
public class User {
private String name;
private int age;
public User() {
}
public User(String name, int age) {
this.name = name;
this.age = age;
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public int getAge(){
return age;
}
public void setAge(int age){
this.age = age;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
<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:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="user" class="com.li.pojo.User" p:name="小华" p:age="18"/>
<bean id="user2" class="com.li.pojo.User" c:name="小李" c:age="20"/>
beans>
bean作用域主要是singleton(单例模式)、prototype、web层面(request、session application、websocket)。
先看看单例模式实现方式
public class Singleton {
public Singleton(){
}
//创建 静态的 唯一的 全局的对象
private static Singleton singleton = new Singleton();
//定义公开的静态方法
public static Singleton getSingleton(){
return singleton;
}
public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
System.out.println(Singleton.getSingleton());
}
System.out.println("===========================");
for (int i = 0; i < 5; i++) {
Singleton singleton = new Singleton();
System.out.println(singleton);
}
}
}
<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:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="user" class="com.li.pojo.User" p:name="小华" p:age="18" scope="singleton"/>
<bean id="user2" class="com.li.pojo.User" c:name="小李" c:age="20" scope="prototype"/>
beans>
测试:
@Test
public void test2(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext2.xml");
User user = context.getBean("user",User.class);
User user2 = context.getBean("user", User.class);
System.out.println(user.hashCode());//265119009
System.out.println(user2.hashCode());//265119009
System.out.println(user == user2);//true
}
public void test2(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext2.xml");
User user = context.getBean("user2",User.class);
User user2 = context.getBean("user2", User.class);
System.out.println(user.hashCode());//1988859660
System.out.println(user2.hashCode());//1514160588
System.out.println(user == user2);//false
}
结论
:
singleton作用域:创建bean的时候对象已经被实例了,并且只有一个对象
prototype作用域:每次去容器取得对象都不同。
场景:一个人有两个宠物!
public class People {
public Dog dog;
public Cat cat;
public String name;
public Dog getDog() {
return dog;
}
public void setDog(Dog dog) {
this.dog = dog;
}
public Cat getCat() {
return cat;
}
public void setCat(Cat cat) {
this.cat = cat;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "People{" +
"dog=" + dog +
", cat=" + cat +
", name='" + name + '\'' +
'}';
}
}
public class Dog {
public void shout(){
System.out.println("wang~");
}
}
public class Cat {
public void shout(){
System.out.println("miao~");
}
}
测试:
public class MyTest {
@Test
public void test() {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
People people = context.getBean("people", People.class);
people.getCat().shout();
people.getDog().shout();
}
}
<!--
如果类的属性中有引用类型需要使用ref标签引用
byName、 byType 可以简化xml代码不用ref标签
-->
<bean id="cat" class="com.li.pojo.Cat"/>
<bean id="dog" class="com.li.pojo.Dog"/>
<bean id="people" class="com.li.pojo.People">
<property name="cat" ref="cat"/>
<property name="dog" ref="dog"/>
</bean>
<!-- byType就是bean的class类型 id也就是对象名随意,没有id也可以-->
<bean class="com.li.pojo.Cat"/>
<bean class="com.li.pojo.Dog"/>
<bean class="com.li.pojo.People" autowire="byType">
</bean>
<!--byName根据id寻找 寻找bean中的peopel对象 -->
<bean id="cat" class="com.li.pojo.Cat"/>
<bean id="dog" class="com.li.pojo.Dog"/>
<bean id="people" class="com.li.pojo.People" autowire="byName">
</bean>
注意点:com.li.pojo.dog 的合格 bean 可用只能匹配单个bean 简单说:就是cat、dog的id名只能有一个,否则报错. Could not autowire. There is more than one bean of 'Dog' type. Beans: dog,dog2. Properties: 'dog' less... (Ctrl+F1) Inspection info:Checks autowiring problems in a Spring bean defined in XML context
使用注解需要扫描指定的包,注解才可以使用
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<!--指定扫描的包注解可以使用-->
<context:annotation-config/>
<context:component-scan base-package="com.li"/>
</beans>
注解:就是一个类,使用@注解名称
开发中:使用注解 取代 xml配置文件。
@Repository
:dao层 位置:类上@Service
:service层 位置:类上@Controller
:web层 位置:类上@Value("")
位置:属性上@Value("小李")
public String name;
引用值:
@Autowired
位置:属性、set方法上 使用@Autowired也可以不写set方法
@Autowired
public Dog dog;
@Autowired
public Cat cat;
@Resource
位置:属性
@Resource
public Dog dog;
@Resource
public Cat cat;
@Autowired 与@Resource可以解决byName 、byType只能匹配单个bean的困扰,也就是说可以引用多个bean,如果没有符合的id会直接通过类寻找
<bean id="cat" class="com.li.pojo.Cat"/>
<bean id="cat22" class="com.li.pojo.Cat"/>
<bean id="cat44" class="com.li.pojo.Cat"/>
<bean id="dog" class="com.li.pojo.Dog"/>
<bean id="dog33" class="com.li.pojo.Dog"/>
<bean id="people" class="com.li.pojo.People">
</bean>
如果想要指定唯一的一个id匹配就使用@Qualifier(value=" ")或者 @Resource(name = " ")
//方式一:
@Autowired
@Qualifier(value = "dog33")
public Dog dog;
@Autowired
@Qualifier(value = "cat22")
public Cat cat;
//方式二:
@Resource(name = "dog33")
public Dog dog;
@Resource(name = "cat22")
public Cat cat;
作用域:
@Scope("singleton") 单例模式
IOC容器启动会调用方法创建对象放到IOC容器中。以后每次获取就是直接从容器(理解成从map.get对象)中拿bean
@Scope("prototype")多例模式
IOC容器启动并不会去调用方法创建对象放在容器中,而是 每次获取的时候才会调用方法创建对象
前面的配置文件都是基于applicationContext.xml文件。现在可以通过java实现容器的配置。具体的配置如下。
@Component//相当于xml文件中bean标签
public class User {
@Value(value = "小米")
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
UserConfig 类等效于以下Spring XML:
@Bean注释被用于指示一个方法实例,可以配置,并初始化到由Spring IoC容器进行管理的新对象
@ComponentScan(basePackages = "com.li.pojo") 等效
<beans>
<context:component-scan base-package="com.li.pojo"/>
beans>
// @Configuation等价于
//@Bean等价于
@Configuration
@ComponentScan(basePackages = "com.li.pojo")
public class UserConfig {
@Bean//相当于bean标签
//方法名就是id
//方法返回值就是class属性
public User getUser(){
return new User();//返回的对象
}
}
public class MyTest {
@Test
public void test(){
//AnnotationConfigApplicationContext(UserConfig.UserConfig) 获取spring容器
ApplicationContext context = new AnnotationConfigApplicationContext(UserConfig.class);
User getUser = context.getBean("getUser", User.class);
System.out.println(getUser.getName());
}
}