我是靠谱客的博主 踏实铅笔,这篇文章主要介绍Spring不同注册方式Spring 5,现在分享给大家,希望可以做个参考。

Spring 5

1.XML 注册

复制代码
1
2
创建applicationContext.xml配置文件
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 例如 构造 service层对象,实现service层对dao层方法的调用 --> <bean id = "userService" class = "com.service.UserService"> <property name = "userDao" ref = "userDao"></property> </bean> <!-- ref 表示注入构造的其他bean对象的值 --> <bean id = “userDao” class = "com.dao.UserDao"></bean> <!-- 引入properties文件 --> <context:property-placeholder location="classpath:db.properties"></context:property-placeholder> <!-- 例如 构造Spring数据库连接对象JdbcTemplate --> <!-- db.properties文件内容如下 --> m.driver=com.mysql.cj.jdbc.Driver m.url=jdbc:mysql://localhost:3306/et2104?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=UTF-8 m.user=root m.pwd=root <!-- 构造数据库JdbcTemplate --> <bean class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="ds"></property> </bean> <bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${m.driver}"></property> <property name="url" value="${m.url}"></property> <property name="username" value="${m.user}"></property> <property name="password" value="${m.pwd}"></property> </bean> </beans>
复制代码
1
2
获取XML中构造的对象
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class Test { public static void main(String[] args) { // 获取spring的工厂类 // XmlBeanFactory 获取 BeanFactory beanFactory = new XmlBeanFactory(new FileSystemResource(new File("E:\Spring_IOC\src\main\resources\applicationContext.xml"))); UserService userService= beanFactory.getBean("userService", UserService.class); // 重写toString()方法 System.out.println(userService.toString()); // 获取spring的工厂类 // ClassPathXmlApplication 非Web程序获取 // ClassPathXmlApplicationContext 获取 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); UserService userService= beanFactory.getBean("userService", UserService.class); // 重写toString()方法 System.out.println(userService.toString()); } }

2.XML + 注解 注册

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
1)构造对象 位置:放在类名处 @Component 代替xml文件中的<bean>标签 @Component 三个衍生注解 @Controller --> controller层 @Service --> service层 @Repository --> dao层 2)依赖注入 注意:必须生成setter方法 位置:放在属性名处 或 set方法处 @Autowired 按类型注入 @Qualifier 指定注入对象的名称 @Resource 平替@Autowired和@Qualifiler xml配置文件内容
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
<?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" 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 https://www.springframework.org/schema/context/spring-context.xsd> <!-- 指定注解依赖的类 --> <context:component-scan base-package="com"></context:component-scan> </beans>
复制代码
1
2
3
对象声明 @Component,@Sercice,@Repository 使用方法类似
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
@Controller // 默认id为类名开头小写,驼峰式 // 例如 此id为studentController public class StudentController { @Autowired @Qualifier("studentServiceImpl1") private StudentService studentService1; private StudentService studentService2; @Resource(name = "studentServiceImpl3") private StudentService studentService3; public StudentService getStudentService1() { return studentService1; } public void setStudentService1(StudentService studentService1) { this.studentService1 = studentService1; } public StudentService getStudentService2() { return studentService2; } @Autowired @Qualifier("studentServiceImpl2") public void setStudentService2(StudentService studentService2) { this.studentService2 = studentService2; } public StudentService getStudentService3() { return studentService3; } public void setStudentService3(StudentService studentService3) { this.studentService3 = studentService3; } }

3.纯 注解 注册

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@Configuration 注解配置类 标识 放在配置类声明处 @ComponentScan(basePackages = " ") 指定注解依赖的类 代替标签<context:component-scan base-package = ""></context:component-scan> @Bean 构造对象声明 代替标签<bean> 位置:放在构造对象的方法声明处 @ImportResource 在注解配置类中引入配置XML @PropertySource("classpath:") 在注解类中引入properties文件 @Value 为属性赋值
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
@Configuration @ComponentScan(basePackages = "com") // 同上数据库配置文件db.properties @PropertySource("classpath:db.properties") public class ETConfig { @Value("${m.driver}") private String driver; @Value("${m.url}") private String url; @Value("${m.user}") private String user; @Value("${m.pwd}") private String pwd; @Bean public JdbcTemplate jt(){ JdbcTemplate jt = new JdbcTemplate(); jt.setDataSource(this.ds()); return jt; } @Bean public DataSource ds() { DriverManagerDataSource ds = new DriverManagerDataSource(); ds.setDriverClassName(this.driver); ds.setUrl(this.url); ds.setUsername(this.user); ds.setPassword(this.pwd); return ds; } }
4.创建对象的不同方法比较
复制代码
1
2
3
4
5
6
1)@Component 和 @Autowired 程序员自己开发的类型 2)@Bean 框架提供的类型(无源码)

5.不同配置方式的优先级

复制代码
1
2
3
4
5
6
7
8
@Component及其衍生注解 < @Bean < XML配置文件bean标签 优先级较高的可覆盖优先级低的配置信息 配置覆盖的前提是id值相同 使用覆盖可更新对象的值 降低程序的耦合性 对扩展开放 对修改关闭 -----开闭原则

最后

以上就是踏实铅笔最近收集整理的关于Spring不同注册方式Spring 5的全部内容,更多相关Spring不同注册方式Spring内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(43)

评论列表共有 0 条评论

立即
投稿
返回
顶部