三种定义bean的方式
- 方法一:基于XML的bean定义(需要提供setter方法)
1.首先编写student.java和teacher.java两个类
Student.java:
public class Student {
private String name;
private Teacher teacher;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Teacher getTeacher() {
return teacher;
}
public void setTeacher(Teacher teacher) {
this.teacher = teacher;
}
}
Teacher.java
public class Teacher {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
2.配置spring.xml基于XML的bean定义(需要提供setter方法)
<?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">
<bean id="student" class="test.Student">
<property name="name" value="张三"/> /设置属性值
<property name="teacher" ref="teacher"/>
</bean>
<bean id="teacher" class="test.Teacher">
<property name="name" value="李四"/> //设置属性值
</bean>
</beans>
- 方法二:基于注解的bean定义(不需要提供setter方法)
@Component("teacher")
public class Teacher {@Value("李四")
private String name;public String getName() {
return name;
}}
@Component("student")
public class Student {@Value("张三")
private String name;@Resource
private Teacher teacher;public String getName() {
return name;
}public Teacher getTeacher() {
return teacher;
}<?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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"><!--扫描组件的包目录-->
<context:component-scan base-package="test"/></beans>
- 方法三:基于Java类的bean定义(需要提供setter方法)
@Configuration
public class BeansConfiguration {@Bean
public Student student(){
Student student=new Student();
student.setName("张三");
student.setTeacher(teacher());
return student;
}@Bean
public Teacher teacher(){
Teacher teacher=new Teacher();
teacher.setName("李四");
return teacher;
}}
入口函数为:
public class Main {
public static void main(String args[]){
AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext(BeansConfiguration.class);
Student student= (Student) context.getBean("student");
Teacher teacher= (Teacher) context.getBean("teacher");
System.out.println("学生的姓名:"+student.getName()+"。老师是"+student.getTeacher().getName());
System.out.println("老师的姓名:"+teacher.getName());
}
}
三种定义bean的方式的更多相关文章
- Spring中三种配置Bean的方式
Spring中三种配置Bean的方式分别是: 基于XML的配置方式 基于注解的配置方式 基于Java类的配置方式 一.基于XML的配置 这个很简单,所以如何使用就略掉. 二.基于注解的配置 Sprin ...
- (转)Spring的三种实例化Bean的方式
http://blog.csdn.net/yerenyuan_pku/article/details/52832793 Spring提供了三种实例化Bean的方式. 使用类构造器实例化. <be ...
- spring三种实例化bean的方式
1构造函数实例化 2静态工厂方法实例化 3实例工厂方法实例化 service接口: package service; public interface PersonService { public v ...
- 三种实例化bean的方式
在spring中有三中实例化bean的方式: 一.使用构造器实例化:(90%通常使用的一个方法) 二.使用静态工厂方法实例化: 三.使用实例化工厂方法实例化. 每种实例化所采用的配置是不一样的: 一. ...
- js中三种定义变量的方式const, var, let的区别。
const var let区别 1.const 定义的变量不可以修改,而且必须初始化 const a = 3;正确 const a;错误,必须初始化 console.log("函数外c ...
- Spring第四弹—–Spring的三种实例化bean的方式
1.使用类构造器实例化 1 <bean id=“orderService" class="cn.itcast.OrderServiceBean"/> 2. ...
- Javascript学习笔记:3种定义函数的方式
①使用函数声明语法定义函数 function sum(num1,num2){ return num1+num2; } ②使用函数表达式定义函数 var sum=function(num1,num2){ ...
- js中三种定义变量 const, var, let 的区别
js中三种定义变量的方式const, var, let的区别 1.const定义的变量不可以修改,而且必须初始化. 1 const b = 2;//正确 2 // const b;//错误,必须初始化 ...
- Request三种获取数据的方式
今天在做ajax请求后台代码时,发现ajax的方法都对,但就是请求不了后台代码,后来在同事帮助下才发现前台定义了两个相同参数导致请求出错. 下面记录一下request三种获取数据的方式: 1. Req ...
随机推荐
- python3 re.compile中含有变量
id = '7F' reg = re.compile(id + '[^\dA-F]?\d') line = ‘122s 7f 3' match = reg.search(line) 在程序中有时候会遇 ...
- sys模块的介绍
sys.argv 命令行参数List,第一个元素是程序本身路径 sys.exit(n) 退出程序,正常退出时exit(0) sys.version 获取 ...
- 并发中的volatile
目录 1. 概述 2. volatile的特性 3. volatile写-读的内存语义 4. volatile内存语义的实现 5. JSR-133为什么要增强volatile的内存语义 6. 总结 1 ...
- Android Studio 3.1.3正式版的新坑。。。
Gradle编译时没问题,运行App时候出现: java.util.NoSuchElementException java.lang.RuntimeException: com.android.bui ...
- 吴裕雄 python深度学习与实践(14)
import numpy as np import tensorflow as tf import matplotlib.pyplot as plt threshold = 1.0e-2 x1_dat ...
- git clone Failed to connect to 127.0.0.1 port 43213: Connection refused
不知道为什么使用git clone 的时候报了上面的错误,后面发现是 127.0.0.1 port 43213的端口被代理占用了,可以这样查看: $ env|grep -i proxy 结果是: NO ...
- android 无法import
参考 https://blog.csdn.net/u012489412/article/details/72784095 File - Invalidate Caches/Restart
- swift - UIButton按钮有图片是点击高亮 有灰色动画
取消 高亮的 动画 btn.adjustsImageWhenHighlighted = false btn.layer.removeAllAnimations()
- 写jsp文件时需要注意的一些小细节
①jsp文件的最开始的部分: <%@ page language="java" contentType="text/html; charset=UTF-8" ...
- bootstrap 辅助工具
模板 https://startbootstrap.com/ 可视化bootstrap在线编辑器 https://www.layoutit.com/