spring可能会迟到,但不会缺席

抱歉了大家,SSM框架不小心遗漏掉了spring框架,在此补上,下一期补springMVC执行原理,下下一期就是springBoot框架了,完了之后,我们一起做一个完整的项目哟,期待哈-------

在这里插入图片描述

一.ioc控制反转

背景引入:我们从service层到dao层来分析
dao层

/UserDao接口

package com.huang.dao;

public interface UserDao {
    void getUser();
}

/UserDao接口实现类1:/UserDaoImpl实现类

package com.huang.dao;

public class UserDaoImpl implements UserDao{
    @Override
    public void getUser() {
        System.out.println("获取普通用户信息");
    }
}

/UserDao接口实现类2:/UserMysqlImpl实现类

package com.huang.dao;

public class UserMysqlImpl implements UserDao{
    @Override
    public void getUser() {
        System.out.println("获取mysql用户信息");
    }
}
service层

/UserService接口

package com.huang.service;

public interface UserService {
    void getUser();
}

/UserService接口实现类:/UserServiceImpl实现类

package com.huang.service;

import com.huang.dao.UserDao;
import com.huang.dao.UserDaoImpl;
import com.huang.dao.UserMysqlImpl;
import org.springframework.beans.factory.annotation.Autowired;


public class UserServiceImpl implements UserService{
	UserDao userDao = new UserDaoImpl();

    @Override
    public void getUser() {
        userDao.getUser();
    }
}
测试类
@Test
public void test1(){
    UserService userService = new UserServiceImpl();
    userService.getUser();
}

问题:当我们的dao层接口有很多很多实现类,我们程序员就必须修改:/UserServiceImpl实现类中的:

UserDao userDao = new UserDaoImpl();

这一段的代码,这样我们的开发做了很多无用功;

我们能不能采用一种方法让用户来决定调用哪个dao层接口实现类呢?

一切对象交由spring管理
我们只需要修改service层接口的实现类:/UserServiceImpl实现类:

注意:ioc依赖注入的方式之一:set注入,所以此处我们一定要给一个set方法

public class UserServiceImpl implements UserService{
    private UserDao userDao;

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    @Override
    public void getUser() {
        userDao.getUser();
    }
}
spring配置文件:
  1. id:bean的唯一标识,后面的class表示类
  2. ref:表示引用类型,通常还有value表示简单类型
  3. 我们在测试类中使用getBean(“userService”);返回的结果就是id为userService对应的类的对象
<?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-3.0.xsd">


    <bean id="userDao" class="com.huang.dao.UserDaoImpl"/>
    <bean id="userMysql" class="com.huang.dao.UserMysqlImpl"/>

    <bean id="userService" class="com.huang.service.UserServiceImpl">
        <property name="userDao" ref="userMysql"/>
    </bean>
    
</beans>
测试类
@Test
public void test2(){
    ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
    UserServiceImpl userService = (UserServiceImpl) context.getBean("userService");
    userService.setUserDao(new UserMysqlImpl());
    userService.getUser();
}

此后调用service层的用户只需要修改以下代码中的dao层接口的实现类

userService.setUserDao(new UserMysqlImpl());

以上代码实现了:从程序员修改代码到用户来传入实现类,这是我对ioc控制反转的理解

二.ioc创建对象的方式(构造方式注入)

1.无参构造

必须有set方法和无参构(默认)

<bean id="hello" class="com.huang.pojo.Hello">
    <property name="address" value="123"></property>
</bean>
public class Hello {
    private String address;
    public void setAddress(String address) {
        this.address = address;
    }
    public void show(){
        System.out.println(address);
    }
}

2.有参构造

<bean id="hello" class="com.huang.pojo.Hello">
    <constructor-arg index="0" value="index"/>
</bean>

<bean id="hello1" class="com.huang.pojo.Hello">
    <constructor-arg type="java.lang.String" value="type"/>
</bean>

<bean id="hello2" class="com.huang.pojo.Hello">
    <constructor-arg name="address" value="name"/>
</bean>
public class Hello {
    private String address;

    public Hello(String address) {
        this.address = address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
    public void show(){
        System.out.println(address);
    }
}

注意:在spring配置文件中注册多个bean时,只要我们执行以下代码:那么所有的bean都被初始化了

ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Hello hello = (Hello) context.getBean("hello1");

三.DI注入创建对象(set注入)

常见数据类型的注入

创建实体类,此处省略get/set方法和toString()方法(一万字)

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 class Address {
    private String address;
}

spring配置文件:

<bean id="address" class="com.huang.pojo.Address">
    <property name="address" value="address1"/>
</bean>

<bean id="student" class="com.huang.pojo.Student">
    <property name="name" value="huangjie"/>
    <property name="address" ref="address"/>
    <property name="books">
        <array>
            <value>book1</value>
            <value>book2</value>
            <value>book3</value>
        </array>
    </property>
    <property name="hobbys">
        <list>
            <value>hobby1</value>
            <value>hobby2</value>
            <value>hobby3</value>
        </list>
    </property>
    <property name="card">
        <map>
            <entry key="card1" value="421281"/>
            <entry key="card2" value="12345"/>
        </map>
        
    </property>
    <property name="games">
        <set>
            <value>lol</value>
            <value>bob</value>
            <value>coc</value>
        </set>
    </property>
    <property name="wife">
        <null/>
    </property>
    <property name="info">
        <props>
            <prop key="info1">200191003</prop>
            <prop key="info2">male</prop>
            <prop key="info3">20</prop>
        </props>
    </property>
</bean>

每个实体类重写toString()然后测试类中测试

四.自动装配

xml配置实现自动装配

一个人有一只猫和一条狗:

看到这句话,我们要能立刻联想到三个类,人,猫,狗,人中有猫和狗两个属性

实体类:

public class Person {
    private Dog dog;
    private Cat cat;
    public Person() {
    }
    public Person(Dog dog, Cat cat) {
        this.dog = dog;
        this.cat = cat;
    }
    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 class Cat {
    private String name = "喵喵";
    public void shut(){
        System.out.println(name);
    }
}
public class Dog {
    private String name = "旺旺";
    public void shut(){
        System.out.println(name);
    }
}

/beans.xml

<bean id="cat" class="com.huang.pojo.Cat"/>
<bean id="dog" class="com.huang.pojo.Dog"/>
<bean id="person" class="com.huang.pojo.Person" autowire="byType"></bean>

自动装配autowire可选值:

  • byname: 需bean的id唯一,并且这个bean需要和自动注入的属性的set方法的值一致
  • bytype:需bean的class唯一,并且这个bean需要和自动注入的属性的类型一致

也就是说:byname和bytype一个取决于id,一个取决于class,两者都有局限性

注解实现自动装配

配置文件:

<?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
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:annotation-config/>
    
	<bean id="cat" class="com.huang.pojo.Cat"/>
    <bean id="dog" class="com.huang.pojo.Dog"/>
    <bean id="dog2" class="com.huang.pojo.Dog"/>
    <bean id="people" class="com.huang.pojo.People"/>
</beans>

实体类:主要看People类

import org.springframework.beans.factory.annotation.Autowired;

public class People {
    @Autowired
    private Cat cat;
    @Autowired
    @Qualifier("dog2")
    private Dog dog;

    public People() {
    }

    public People(Cat cat, Dog dog) {
        this.cat = cat;
        this.dog = dog;
    }

    public Cat getCat() {
        return cat;
    }

    public Dog getDog() {
        return dog;
    }
}
  • @Autowired注解也可以加在set方法上,加在属性上可以省略set方法
  • @Autowired默认按照bytype来装配,如果要指定按byname装配,我们一般配合@Qualifier(“dog2”)来使用

五.spring注解开发

/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"
       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-3.0.xsd">
       
	//扫描包 如果在com.huang下还有Dao,service,controller层,base-package值应该改为com.huang
    <context:component-scan base-package="com.huang.pojo"/>
    //开启注解支持
    <context:annotation-config/>

</beans>

实体类

@Component   //等同于 <bean id="teacher" class="com.huang.pojo.Teacher "/>
@Scope("prototype")
public class Teacher {

    private String name;

    @Value("黄老师")  //等同于 <property name="name" value="黄老师"/>
    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
}
  • @Scope(“prototype”) 指定单例模式
  • @Value(“黄老师”)可以放在属性上,此时set方法可以省;也可以放在set方法上

测试类

context.getBean中bean的名字默认为类首字母小写

@Test
public void test7(){
    ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
    Teacher teacher = context.getBean("teacher",Teacher.class);
    System.out.println(teacher.getName());
}

类似@Component的注解:都是代表将莫个类注册到spring容器中并装配

  • Dao层:@Repository
  • Service层:@Service
  • Controller层:@Controller

在SSM项目整合和springBoot阶段在来总结常见注解

六.spring切面编程(待续)

七.spring整合mybatis(之前博客有)

下一阶段总结springMVC的执行原理,完了之后开始springBoot之旅

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值