Spring框架之SpringIOC

Spring的介绍

(1)Spring是什么?
Spring 是分层的 Java SE/EE 应用 full-stack 轻量级开源框架
full-stack 全栈
轻量级按需添加模块
开源 可以获取源代码
以 IOC- (Inverse Of Control:反转控制)和 AOP- (Aspect Oriented Programming:面向切面编程)为内核
(2)有什么特点?
提供了展现层 SpringMVC
持久层 Spring JDBC
还能整合开源世界众多著名的第三方框架和类库
业务层事务管理 AOP
方便解耦,简化开发 IOC
Java源码是经典学习范例
逐渐成为使用最多的 Java EE 企业应用开源框架

Spring架构体系

在这里插入图片描述

  1. Test:用于测试使用
  2. Core container:核心容器,就是用于装Java Bean对象
  3. AOP:切面编程
    4.Aspects:提供了与AspectJ的集成
  4. Data access:数据访问。用于访问操作我们的数据库。支持持久层的操作。jdbcTemplate mybatis
  5. Web:用于支持数据展示层,支持http请求
  6. Transactions:用于支持事物处理。用于解决业务层的事物处理问题。 编程式事务管理和声明式事务管理.

Spring IOC

  1. 控制反转(Inversion of Control,缩写为IoC),是面向对象编程中的一种设计原则,可以用来减低计算机代码之间的耦合度其中最常见的方式叫做依赖注入(Dependency Injection,简称DI),还有一种方式叫“依赖查找”(Dependency Lookup)。通过控制反转,对象在被创建的时候,由一个调控系统内所有对象的外界实体,将其所依赖的对象的引用传递给它。也可以说,依赖被注入到对象中
    简单说:把创建对象和管理对象的权利交给spring
    在这里插入图片描述

Spring的IOC入门-环境搭建

(1)创建Project maven
(2)创建模块module maven
(3)配置依赖

<!--spring依赖 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.9.RELEASE</version>
        </dependency>

Spring的IOC入门-代码编写

(1)定义Person类
(2)手动完成创建与赋值
(3)由spring创建与赋值
》创建容器对象
》读配置文件
new ClassPathXmlApplicationContext(“applicationContext.xml”);
》从容器中查找getBean()

Test01SpringIoc

public class Test01SpringIoc {
    @Test
    public void test01(){
        //1:创建ioc 容器对象  暂时看成map
        ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        //2:给定配置文件的名称 applicationContext.xml
        //3:调用容器的getBean方法获取id对应的对象
        Person person = (Person) context.getBean("person");
        System.out.println(person);
    }
}

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">

<!--    要让 spring容器给我创建一个person对象-->
<!--    配置类名,用于反向创建对象-->
<!--    同时给一个编号方便查找-->
    <bean id="person" class="com.wzx.domain.Person" />
</beans>

Spring的IOC入门-问题解答

(1)方法区别
context.getBean(“id值”, 类型.class);//无需转型
context.getBean(“id值”);//需转型

context.getBean("id值", 类型.class);//无需转型
context.getBean("id值");//需转型

(2)bean标签的属性
id:bean标签的识别ID,理论上可以随便写
class:你要上Spring给你创建哪个类的对象,需要写上该类的全路径名

 public void test01(){

        //3:调用容器的getBean方法获取id对应的对象
        Person person = (Person) context.getBean("person2");
        //Person person2 = context.getBean("person2",Person.class);
        log.debug(person.toString());
    }
     @Test
    public void test02(){
        Person person1 =  new Person();
        //设置方法
        person1.setId(1);
        log.debug(person1.toString());
        //构造方法赋值
        Person person2 =  new Person(1,"jack",20,new Date());
        log.debug(person2.toString());
    }

name:成员变量的名字
value:成员变量的值
一个property标签最后会调一个对应的set方法

 <bean id="person2" class="com.wzx.domain.Person" >
        <property name="id" value="10"/>
        <property name="name" value="rose"/>
        <property name="age" value="20"/>
    </bean>

通过构造方法创建对象

<!--    Person person2 =  new Person(1,"jack",20,new Date());-->
<!--    System.out.println(person2);-->
    <bean id="date1" class="java.util.Date"/>
    <bean id="person3" class="com.wzx.domain.Person" >
            <constructor-arg name="id" value="10"/>
            <constructor-arg name="name" value="hello"/>
            <constructor-arg name="age" value="20"/>
            <constructor-arg name="birthday" ref="date1"/>
    </bean>

配置构造方法的参数的
constructor-arg 如果有四个,就表示调的一个四个参数的构造方法。
value可以赋上基本类型数据与String,但是其他对象,要使用ref
表示在当前容器中查找一个已存在的对象

Spring依赖注入-Xml方式 Dao

  • 1)对象注入
public class A{
   private int id;
   private B b;
}
public class XXXService{
   private int id;
   private XxxDao xxxdao;
}

Test

 @Test
    public void test09(){
       //PersonService personService = new PersonService();
       PersonService personService = (PersonService) context.getBean("personService");
       Person p  = new Person();
       p.setUsername("jack");
       p.setPassword("12345");
       boolean flag =personService.login(p);
       log.debug(flag+"");
    }

PersonService

public class PersonService {
    private static  final Logger log= LoggerFactory.getLogger(PersonService.class);
    //private PersonDao personDao = new PersonDao();
    private PersonDao personDao ;

    public void setPersonDao(PersonDao personDao) {
        this.personDao = personDao;
    }

    public boolean login(Person p) {
        log.debug(p+" login");
        Person person = personDao.find(p);
        if(person==null) {
            return false;
        }else{
            return true;
        }
    }
}

PersonDao

public class PersonDao {
    public Person find(Person p) {
        if("jack".equals(p.getUsername())&&"12345".equals(p.getPassword())){
            return p;
        }else{
            return null;
        }
    }
}

applicationContext.xml

    <bean id="personService" class="com.wzx.service.PersonService">
            <property name="personDao" ref="personDao"/>
    </bean>
    <bean id="personDao" class="com.wzx.dao.PersonDao">
    </bean>

Spring依赖注入-注解创建对象

(1)对象比较多的话,开启注解扫描

<?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.xsd">
    <!--
        使用注解方式进行创建对象
        1.开启注解扫描

        含义:开启注解扫描,指定了 base-package 扫描指定的包,扫描包与子包中所有的类
        查看类上是否有指定的注解, 如果类上有指定的注解,那么就创建给类对象,
        放到spring容器中
    -->
    <context:component-scan base-package="com.wzx"/>
    </beans>

只有标记有注解的类,才会被创建对象且添加到ioc容器中
(3)四个注解
@Component //其他层(可以用于所有层)
@Repository //Dao层
@Service //Service层
@Controller(“xxx”)//Controller层

//@Component  //其他层
//@Repository //Dao层
//@Service    //Service层
@Controller("xxx")//Controller层
public class MyClass{
}

注解没有配置id,但是默认是 myClass

    @Test
    public void test10(){

        PersonService personService = (PersonService) context.getBean("personService");
        log.debug(personService+" test10");

        PersonDao personDao = (PersonDao) context.getBean("personDao");//id为类名首字符小写
        log.debug(personDao +" test10");
    }

Spring依赖注入-注解实现注入

(1)注入是什么?
就是查找之后,进行赋值
(2)三种注入方式
1
@Autowired
或者
@Autowired
@Qualifier(“bean的id”)
2
@Value("#{bean的id}")
3
@Resource(name=“bean的id值”)

@Service
public class PersonService {
   //private PersonDao personDao = new PersonDao();
    //第一种:@Autowired或者  @Autowired和@Qualifier("bean的id")搭配
    //第二种:@Value("#{bean的id}")
    //第三种:@Resource(name="bean的id值")
    @Autowired
    PersonDao personDao ;
 }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值