没有合适的资源?快使用搜索试试~ 我知道了~
做springboot项目期间总结了一下,可能不完善,但是对于初学者应该是有用的
资源推荐
资源详情
资源评论
conguration 注解
从 , 用于定义配置类,可替换 配置文件,被注解
的类内部包含有一个或多个被 注解的方法,这些方法将会被
或
类进行扫描,并用于构建 定义,
初始化 容器。
注意: 注解的配置类有如下要求:
不可以是 类型;
不可以是匿名类;
嵌套的 必须是静态类。
一、用 加载
、 配置 并启动 容器
、 启动容器 注册
、 启动容器 注册
、使用 AnnotationConfigApplicationContext 注册 AppContext 类的两
种方法
1.5、配置 Web 应用程序(web.xml 中配置
AnnotationConfigApplicationContext)
二、组合多个配置类
、在 中引入 的 配置文件
、在 中引入其它注解配置
、 嵌套(嵌套的 必须是静态类)
三、 注解
四、! 逻辑组配置
五、使用外部变量
一、@Configuation 加载 Spring 方法
1.1、@Configuration 配置 spring 并启动 spring 容器
标注在类上,相当于把该类作为 的 配置文件中的
<beans>,作用为:配置 容器"应用上下文#
package com.dxz.demo.configuration;
import org.springframework.context.annotation.Configuration;
@Configuration
public class TestConfiguration {
public TestConfiguration() {
System.out.println("TestConfiguration 容器启动初始化。。。");
}
}
相当于:
<?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"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-
4.0.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.0.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-4.0.xsd"
default-lazy-init="false">
</beans>
主方法进行测试:
package com.dxz.demo.configuration;
import org.springframework.context.ApplicationContext;
import
org.springframework.context.annotation.AnnotationConfigApplicatio
nContext;
public class TestMain {
public static void main(String[] args) {
// @Configuration 注解的 spring 容器加载方式,用
AnnotationConfigApplicationContext 替换
ClassPathXmlApplicationContext
ApplicationContext context = new
AnnotationConfigApplicationContext(TestConfiguration.class);
// 如果加载 spring-context.xml 文件:
// ApplicationContext context = new
// ClassPathXmlApplicationContext("spring-context.xml");
}
}
从运行主方法结果可以看出, 容器已经启动了:
1.2、@Configuration 启动容器+@Bean 注册
Bean,@Bean 下管理 bean 的生命周期
标注在方法上"返回某个实例的方法#,等价于 的 配置文件中
的<bean>,作用为:注册 对象
类:
package com.dxz.demo.configuration;
public class TestBean {
private String username;
private String url;
private String password;
public void sayHello() {
System.out.println("TestBean sayHello...");
}
public String toString() {
return "username:" + this.username + ",url:" + this.url +
",password:" + this.password;
}
public void start() {
System.out.println("TestBean 初始化。。。");
}
public void cleanUp() {
System.out.println("TestBean 销毁。。。");
}
}
配置类:
package com.dxz.demo.configuration;
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("TestConfiguration 容器启动初始化。。。");
}
// @Bean 注解注册 bean,同时可以指定初始化和销毁方法
//
@Bean(name="testBean",initMethod="start",destroyMethod="cleanUp")
@Bean
@Scope("prototype")
public TestBean testBean() {
return new TestBean();
}
}
剩余18页未读,继续阅读
资源评论
__fu
- 粉丝: 1
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功