牛客网项目笔记-IOC入门

  1. 注意点:用spring initializer ,在下载AOP依赖包时,找不到,在依赖包中另外添加AOP依赖,再重新编译执行时,也会报错。(暂时先不管,是否是由于组件更新了?)
  • Spring Boot核心作用
    起步依赖、自动配置、端点监控。
  1. 改tomcat服务器端口
    在resources底下的application.properties
    server.port=8080
    配置项目访问路径
    server.servlet.context-path=/community

  2. Spring全家桶

  • Spring Framework.
  • Spring Boot
  • Spring Cloud
  • Spring Cloud Data Flow
  1. Spring Framework.
  • Spring Core
    • loC、AOP.
  • Spring Data Access
    • Transactions、Spring MyBatis.
  • Web Servlet
    • Spring MVC.
  • lntegration
    • Email、Scheduling、AMQP、Security
  1. 官方文档
    https://docs.spring.io/spring-boot/docs/current/reference/html/

在这里插入图片描述


在这里插入图片描述

容器创建以后,会自动扫描bean,将bean装配到容器里
其中,SpringBootApplication表示一个配置文件的类,里面启用了自动配置,组件扫描,装配bean。会扫描:配置类所在的包以及子包下的bean. (被扫描的bean也需要有注解)

在这里插入图片描述


被扫描的类


有四个注解可以使用(service,controller,repository, component):其他都是由component实现。


在测试类中执行代码,运行程序。添加注解,启用配置类

在这里插入图片描述


如何得到容器:

在这里插入图片描述

@SpringBootTest
@ContextConfiguration(classes = CommunityApplication.class)
class CommunityApplicationTests implements ApplicationContextAware {//implements ApplicationContextAware 得到容器
	private ApplicationContext applicationContext;
	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		//传入的这个参数ApplicationContext就是一个容器,是一个接口,
		// 容器暂存了下来,就可以使用这个容器了
		this.applicationContext=applicationContext;
	}
	@Test
	public void testApplicationContext(){
		System.out.println(applicationContext);//查看这个容器有没有值,证明这个容器是存在,可用的
	}

接下来使用容器管理bean

  1. 创建存放访问数据库的bean的包dao
  2. 创建bean

    在这里插入图片描述

  3. 使用bean,管理Bean
	@Test
	public void testApplicationContext(){
		System.out.println(applicationContext);//查看这个容器有没有值,证明这个容器是存在,可用的
		//从容器获取自动装配的bean,要指明特定的类型
		AlphaDao alphaDao=applicationContext.getBean(AlphaDao.class);
		System.out.println(alphaDao.select());
	}


添加业务组件
Bean在容器中被管理的过程

@Service
public class AlphaService {

    public AlphaService(){
        System.out.println("实例化AlphaService:构造方法中");
    }
    @PostConstruct//在构造器之后调用,初始化某些数据
    public void init(){
        System.out.println("初始化AlphaService 想让容器管理此方法");
    }
    //类的销毁方法
    @PreDestroy//在对象销毁之前调用
    public void destroy(){
        System.out.println("销毁AlphaService-在对象销毁之前调用:可以在这里释放某些资源");
    }
}

Bean默认是单例的,只有一个实例,会销毁一次
当然也可以设置为不是单例模式的:每次获取Bean都是重新实例化一个bean对象

在类之前添加,@Scope(“prototype”)//默认参数是single,若为多例模式,则要加上prototype,则每次访问bean创造一个新的实例

在这里插入图片描述

	@Test
	public void testBeanManagement(){
		//获取Bean
		AlphaService alphaService=applicationContext.getBean(AlphaService.class);
		System.out.println(alphaService);

		alphaService=applicationContext.getBean(AlphaService.class);
		System.out.println(alphaService);
	}


如何将对三方的bean装配到容器中去,(在jar中,不一定有源码,不可修改等)
需要写配置类,通过注解实现

@Configuration//表示此类是一个配置类,不是一个普通的类
public class AlphaConfig {
    @Bean
    public SimpleDateFormat simpleDateFormat(){
        //simpleDateFormat词方法名就是一个bean的名字
        //方法返回的对象,会被装配到容器中
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    }
}

@Test
	public void testBeanConfig(){
		SimpleDateFormat simpleDateFormat=applicationContext.getBean(SimpleDateFormat.class);
		System.out.println(simpleDateFormat.format(new Date()));
	}

更简洁的方法获取bean

	@Autowired//声明:给当前的bean注入到容器中
	private AlphaDao alphaDao;
	@Test
	public void testDI(){//依赖注入
		System.out.println(alphaDao);
	}
@Autowired//声明:给当前的bean注入到容器中
	@Qualifier("alphaHibernate")//spring把alphaHibernate注入到bean,就不是默认的mybatis了,与低层的实现就无关了,降低了耦合度
	private AlphaDao alphaDao;

	@Autowired
	private AlphaService alphaService;

	@Autowired
	private SimpleDateFormat simpleDateFormat;
	@Test
	public void testDI(){//依赖注入
		System.out.println(alphaDao);
		System.out.println(alphaService);
		System.out.println(simpleDateFormat);
	}

相关文章

学习编程是顺着互联网的发展潮流,是一件好事。新手如何学习...
IT行业是什么工作做什么?IT行业的工作有:产品策划类、页面...
女生学Java好就业吗?女生适合学Java编程吗?目前有不少女生...
Can’t connect to local MySQL server through socket \'/v...
oracle基本命令 一、登录操作 1.管理员登录 # 管理员登录 ...
一、背景 因为项目中需要通北京网络,所以需要连vpn,但是服...