Spring的部分: 使用注解之前要开启自动扫描功能 其中base-package为需要扫描的包(含子包). <context:component-scan base-package="cn.test"/> 1.声明bean的注解 @Component 组件,没有明确的角色 @Service 在业务逻辑层使用(service层) @Repository 在数据访问层使用(dao层) @Controller 在展现层使用,控制器的声明(C) 2.注入bean的注解 @Auto…
配置QueryRunner对象:注解说明 package cn.mepu.config; import org.apache.commons.dbutils.QueryRunner; import org.springframework.context.annotation.*; import javax.sql.DataSource; /** * * @author shkstart * @create 2019-11-08 13:49 * 该类是配置类,和bean.xml是一样的 * Spr…
Bean相关的配置: <bean>标签的id和name的配置: id:使用了约束中的唯一约束,里面不能出现特殊字符 name:没有使用唯一约束,理论上可以重复,实际上开发不行,里面可以出现特殊字符 Bean的生命周期的配置: init-method :Bean被初始化的时候执行的方法 destroy-method:Bean被销毁的时候执行的方法(要求Bean是单例执行,默认单例)…
1.属性注入 (1)根据setter方法属性注入,这里使用的是property标签.需要bean属性提供对应的setter方法,比如笔记(1)里的 HelloWorld使用的就是这种方法. <!-- 根据setter方法属性注入 --> <bean id="helloworld" class="com.broadtext.beans.Helloworld"> <property name="name" value=&…
基本用途从Spring3.0,@Configuration用于定义配置类,可替换xml配置文件,被注解的类内部包含有一个或多个被@Bean注解的方法,这些方法将会被AnnotationConfigApplicationContext或AnnotationConfigWebApplicationContext类进行扫描,并用于构建bean定义,初始化Spring容器.例如: @Configurationpublic class AppConfig { @Bean public MyBean myB…
1. Bean依赖关系 一个配置类的Bean,一个实例Bean: 实例Bean初始化时需要依赖配置类的Bean: 1.1 配置类Bean @ConfigurationProperties(prefix = "system") public class SystemConfig { private Integer type; private String rootPath; } 1.2 实例Bean @Component public class HDFSFileHandler impl…
对于学习spring有帮助的站点:http://jinnianshilongnian.iteye.com/blog/1482071 Bean的自己主动装配 Spring IOC 容器能够自己主动装配 Bean. 需要做的仅仅是在 的 autowire 属性里指定自己主动装配的模式 有以下几种自己主动装配的类型: byType(依据类型自己主动装配): 若 IOC 容器中有多个与目标 Bean 类型一致的 Bean. 在这样的情况下, Spring 将无法判定哪个 Bean 最合适该属性, 所以不…
基于Java配置选项,可以编写大多数的Spring不用配置XML,但有几个基于Java的注释的帮助下解释.从Spring3.0开始支持使用java代码来代替XML来配置Spring,基于Java配置Spring依靠Spring的JavaConfig项目提供的很多优点.通过使用@Configuration, @Bean ,@Import ,@DependsOn 来实现Java配置Spring. 1) @Configuration & @Bean 注解: 在Spring的新的Java-Configu…
1.@Configuration标注在类上,相当于把该类作为spring的xml配置文件中的<beans>,作用为:配置spring容器(应用上下文) package com.test.spring.support.configuration; @Configuration public class TestConfiguration { public TestConfiguration(){ System.out.println("spring容器启动初始化...");…
1.@Configuration标注在类上,相当于把该类作为spring的xml配置文件中的<beans>,作用为:配置spring容器(应用上下文) package com.test.spring.support.configuration; @Configuration public class TestConfiguration { public TestConfiguration(){ System.out.println("spring容器启动初始化...");…