20221017 Spring5 IOC

spring 学习

实体类(DI、IOC)

bean

//默认执行单例模式,饿汉加载,执行构造方法
<bean class="" name=""></bean>
public class ClassCreateTest {

    ClassPathXmlApplicationContext applicationContext;
    @Before
    public void init(){
    	//加载配置文件,在加载的过程中实例化bean
        applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
    }

    @Test
    public void create(){
        Object book = applicationContext.getBean("book");

        Object book1 = applicationContext.getBean("book");
        
        System.out.println(book==book1);//true
    }
}

修改非单例模式
使用bean中的scope属性修改模式

  • singleton单例:每次取出的bean都是同一个bean。默认就是这个
  • prototype原型:每次取的bean时,都会重新创建一个新的bean ,是懒加载.
  • request:一次请求一个bean定义对应一个实例(web相关)
  • session:一个session定义对应一个实例(web相关)
  • globalsession类似于session作用域,只是其用于portlet环境的web应用。如果在非portlet环境将视为session作用域(web相关)

prototype不仅修改成原型模式,还修改为懒加载
在这里插入图片描述

在单例模式下修改懒加载
使用lazy-init属性修改为true
在这里插入图片描述
属性注入和构造方法注入

<bean class="com.zzm.entity.Book" name="book">
<!--        属性注入方式,又被称setter注入,需要创建属性的set方法,注入简单属性-->
        <property name="" value=""></property>

<!--        属性注入方式,注入Java对象-->
        <property name="">
            <list>
<!--                自定义对象对象-->
                <ref bean=""></ref>
<!--                简单属性-->
                <value></value>
            </list>
        </property>
        
        <property name="">
            <map>
                <entry key="" value=""></entry>
            </map>
        </property>
<!--        属性注入方式,自定义对象注入 ref写入bean的name或者id-->
        <property name="" ref=""></property>



<!--        构造方法注入,需要创建对应参数的构造方法,有几个参数写几个constructor-arg-->
        <constructor-arg name="" value=""></constructor-arg>
        <constructor-arg name="">
            <list>
<!--                自定义对象对象-->
                <ref bean="beanname or beanid"></ref>
<!--                简单属性-->
                <value></value>
            </list>
        </constructor-arg>
        <constructor-arg name="" ref="beanname or beanid"></constructor-arg>
        
    </bean>

自动注入

在这里插入图片描述

SpringTest

使用@SpringJUnitConfig()执行配置文件
@Autowired 实例化 bean

@SpringJUnitConfig(locations = "classpath:applicationContext.xml")
public class ClassCreateTest {
    
    //这部分代码被 @SpringJUnitConfig 标签代替
//    ClassPathXmlApplicationContext applicationContext;
//    @Before
//    public void init(){
//        applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
//    }

    //使用@Autowired标签代替 applicationContext.getBean()方法
    @Autowired
    //对象的名字参考bean设置的name或者id
    //如果实体类对应的bean只有一个,那么对象的名字没有要求
    //如果bean有多个,那么对象的名字就需要和bean的name或者id一致
    private Book book;
    private Book book1;
    
    @Test
    public void create(){
//        Object book = applicationContext.getBean("book");
//        Object book1 = applicationContext.getBean("book");

        System.out.println(book==book1);
    }
}

使用context标签简化实体类

配置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:aop="http://www.springframework.org/schema/aop"
       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-4.3.xsd
      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">

    <!--加入注解的支持-->
    <context:annotation-config></context:annotation-config>
    <!--扫描指定的路径-->
    <context:component-scan base-package="包的最小基础路径 :如 com.etc"></context:component-scan>


</beans>

加上标签

  1. 在类上使用@Component注解,表示该类定义为Spring管理Bean,使用默认value(可选)属性表示Bean标识符。
  2. @Repository:@Component扩展,被@Repository注解的POJO类表示DAO层实现,从而见到该注解就想到DAO层实现,使用方式和@Component相同;
  3. @Service:@Component扩展,被@Service注解的POJO类表示Service层实现,从而见到该注解就想到Service层实现,使用方式和@Component相同;
  4. @Controller:@Component扩展,被@Controller注解的类表示Web层实现,从而见到该注解就想到Web层实现,使用方式和@Component相同;放在Action前.
@Repository
public class GoodsDaoImpl  implements GoodsDao
@Autowired
private GoodsDao goodsdao
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值