Spring笔记

Spring学习阶段记得一些笔记和自己的理解,因为有些地方加入了自己的理解,所以有不对的地方还请前辈们指正!非常感谢!
自己也会随着学习的深入时不时回头看一下有没有当初理解错的地方去更正
学习时参考链接已经放在知识点下面,参考书籍:JAVAEE 开发的颠覆者:Spring Boot 实战,spring in action

1.@bean如果不指定为原型作用域,则默认是单例作用域,分别可以通过@Scope(“prototype”)和@Scope(“Singleton”)指定

参考:
https://www.cnblogs.com/lizhonghua34/p/4953500.html
https://www.cnblogs.com/duanxz/p/7493276.html

package com.wisely.ch5_2_4;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

@Configuration
public class TestConfiguration {
    public TestConfiguration()
    {
        System.out.println("配置加载成功");
    }
    @Bean
    //指定为原型作用域
    @Scope("prototype")
    public TestBean testBean()
    {
        return new TestBean();
    }

}

2.可以通过ApplicationContext创建一个Spring容器

引用自:
https://blog.csdn.net/tinyDolphin/article/details/75087551

Spring 自带了多种类型的应用上下文。

AnnotationConfigApplicationContext:从一个或多个基于 Java 的配置类中加载 Spring 应用上下文。
AnnotationConfigWebApplicationContext:从一个或多个基于 Java 的配置类中加载 Spring Web 应用上下文。
ClassPathXmlApplicationContext:从类路径下的一个或多个 XML 配置文件中加载上下文定义,把应用上下文的定义文件作为类资源。
FileSystemXmlapplicationcontext:从文件系统下的一个或多个 XML 配置文件中加载上下文定义。
XmlWebApplicationContext:从 Web 应用下的一个或多个 XML 配置文件中加载上下文定义。

// 从指定的文件系统路径下,查找 knight.xml 文件
ApplicationContext context = new FileSystemXmlApplicationContext("d:/knight.xml");

// 使用 ClassPathXmlApplicationContext 从应用的类路径下加载应用上下文
ApplicationContext context = new ClassPathXmlApplicationContext("knight.xml");

// 从 Java 配置中加载应用上下文
ApplicationContext context = new AnnotationConfigApplicationContext(com.springinaction.knights.config.KnightConfig.class);

3.@ComponentScan自动扫描包名下的@Service、@Component、@Repository、@Controller类并注册bean

TestBean

package com.wisely.ch5_2_4;

import org.springframework.stereotype.Component;

@Component
public class TestBean {
    private String username;
    private int age;
    @Override
    public boolean equals(Object o)
    {
        if(this == o){return true;}
        if(null==o||this.getClass()!=o.getClass()){return false;}
        TestBean tb = (TestBean)o;
        return this.username==tb.username&&this.age==tb.age;
    }
    public TestBean()
    {
        System.out.println("TestBean");
    }
}

配置类TestConfiguration

package com.wisely.ch5_2_4;


import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan("com.wisely.ch5_2_4")
public class TestConfiguration {
    public TestConfiguration()
    {
        System.out.println("配置加载成功");
    }
}

事实上不配置自动扫描需要这样去写
TestBean

package com.wisely.ch5_2_4;

public class TestBean {
    private String username;
    private int age;
    @Override
    public boolean equals(Object o)
    {
        if(this == o){return true;}
        if(null==o||this.getClass()!=o.getClass()){return false;}
        TestBean tb = (TestBean)o;
        return this.username==tb.username&&this.age==tb.age;
    }
    public TestBean()
    {
        System.out.println("TestBean");
    }
}

配置类TestConfiguration

package com.wisely.ch5_2_4;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

@Configuration
public class TestConfiguration {
    public TestConfiguration()
    {
        System.out.println("配置加载成功");
    }
    @Bean
    @Scope("prototype")
    public TestBean testBean()
    {
        return new TestBean();
    }

}

4.实现一个简单的SpringJUnit4单元测试

pom文件中添加依赖

<dependency><!--这里是spring-boot的解决办法-->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
</dependency>

Junit代码

import com.wisely.ch5_2_4.TestBean;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import static org.junit.Assert.assertNotNull;

@RunWith(SpringJUnit4ClassRunner.class)
//如果是通过xml配置bean的方式通过locations = {"classpath:springconfig/spring.xml"}生成一个应用上下文
//如果是JAVA配置bean的方式使用 classes=*****.class 其中*****代表JAVA配置bean的类名
@ContextConfiguration(locations = {"classpath:springconfig/spring.xml"})
public class JunitTest {
    @Autowired
    private TestBean tb;
    @Test
    public void testBeanNotBeNull()
    {
        assertNotNull(tb);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值