前言
| (1)基于 xml 配置文件方式实现 |
| (2)基于注解方式实现 |
| |
| |
| |
| <bean id="user" class="com.ychen.spring.User"></bean> |
| |
| |
| |
| * id 属性:唯一标识 |
| * class 属性:类全路径 |
| |
| |
| |
| |
| ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml"); |
| User user = context.getBean("user", User.class); |
| # 在实体类中编写有参构造方法 |
| public class User { |
| |
| private String name; |
| |
| public User(String name) { |
| this.name = name; |
| } |
| |
| public void add() { |
| System.out.println("add......"); |
| } |
| |
| } |
| |
| |
| # xml配置 |
| <bean id="user" class="com.ychen.spring.model.User"></bean> |
| |
| # 执行test方法 |
| @Test |
| public void testAdd() { |
| ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml"); |
| User user = context.getBean("user", User.class); |
| System.out.println(user); |
| user.add(); |
| } |
| |
| # 控制台报错 |
| 4月 17, 2022 9:41:50 上午 org.springframework.context.support.AbstractApplicationContext refresh |
| 警告: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'user' defined in class path resource [bean1.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.ychen.spring.model.User]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.ychen.spring.model.User.<init>() |
| |
| org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'user' defined in class path resource [bean1.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.ychen.spring.model.User]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.ychen.spring.model.User.<init>() |
| |
| # 错误原因:实体类中是有参构造方法,而在bean.xml中只能通过无参构造方法创建对象 |
使用set方法注入
| # 新建1个实体类Book |
| public class Book { |
| |
| private String bname; |
| |
| private String bauthor; |
| |
| public void setBname(String bname) { |
| this.bname = bname; |
| } |
| |
| public void setBauthor(String bauthor) { |
| this.bauthor = bauthor; |
| } |
| |
| |
| public static void main(String[] args) { |
| Book book = new Book(); |
| book.setBauthor("goudan"); |
| System.out.println(book); |
| } |
| |
| } |
| |
| # main方法中测试 |
| com.ychen.spring.model.Book@5b464ce8 |
| |
| Process finished with exit code 0 |
| # 编写实体类 |
| public class Book { |
| |
| private String bname; |
| |
| private String bauthor; |
| |
| private String address; |
| |
| public void setBname(String bname) { |
| this.bname = bname; |
| } |
| public void setBauthor(String bauthor) { |
| this.bauthor = bauthor; |
| } |
| |
| public void setAddress(String address) { |
| this.address = address; |
| } |
| |
| public void testDemo() { |
| System.out.println(bname+"::"+bauthor+"::"+address); |
| } |
| |
| } |
| |
| # 编写bean.xml |
| <bean id="book" class="com.ychen.spring.model.Book"> |
| <property name="bname" value="易筋经"></property> |
| <property name="bauthor" value="达摩老祖"></property> |
| </bean> |
| |
| # 编写测试方法 |
| @Test |
| public void testBook1() { |
| ApplicationContext context = |
| new ClassPathXmlApplicationContext("bean1.xml"); |
| Book book = context.getBean("book", Book.class); |
| System.out.println(book); |
| book.testDemo(); |
| } |
| |
| # 测试 |
| com.ychen.spring.model.Book@2cb4893b |
| 易筋经::达摩老祖::null |
使用构造方法注入
| # 编写实体类 |
| public class Orders { |
| |
| private String oname; |
| |
| private String address; |
| |
| public Orders(String oname,String address) { |
| this.oname = oname; |
| this.address = address; |
| } |
| |
| public static void main(String[] args) { |
| Orders orders = new Orders("aaa", "bbb"); |
| System.out.println(orders); |
| } |
| |
| } |
| |
| # 测试结果 |
| com.ychen.spring.model.Orders@57829d67 |
| |
| Process finished with exit code 0 |
| # 编写实体类 |
| public class Orders { |
| |
| private String oname; |
| |
| private String address; |
| |
| public Orders(String oname,String address) { |
| this.oname = oname; |
| this.address = address; |
| } |
| |
| public void ordersTest() { |
| System.out.println(oname+"::"+address); |
| } |
| |
| } |
| |
| # 配置bean.xml |
| <bean id="orders" class="com.ychen.spring.model.Orders"> |
| <constructor-arg name="oname" value="电脑"></constructor-arg> |
| <constructor-arg name="address" value="China"></constructor-arg> |
| </bean> |
| |
| # 编写测试类 |
| @Test |
| public void testOrders() { |
| ApplicationContext context = |
| new ClassPathXmlApplicationContext("bean1.xml"); |
| Orders orders = context.getBean("orders", Orders.class); |
| System.out.println(orders); |
| orders.ordersTest(); |
| } |
| |
| # 测试 |
| Connected to the target VM, address: '127.0.0.1:58847', transport: 'socket' |
| com.ychen.spring.model.Orders@6b695b06 |
| 电脑::China |
| Disconnected from the target VM, address: '127.0.0.1:58847', transport: 'socket' |
| |
| Process finished with exit code 0 |
| |
| # 注意:这里的实体类中虽然写的是有参构造方法,创建对象时没有报错 |
| # 这是因为在使用有参构造注入属性时,是使用有参构造创建对象的 |
set方法注入属性简写:p名称空间注入
| # 实体类 |
| public class Book { |
| |
| private String bname; |
| |
| private String bauthor; |
| |
| private String address; |
| |
| public void setBname(String bname) { |
| this.bname = bname; |
| } |
| public void setBauthor(String bauthor) { |
| this.bauthor = bauthor; |
| } |
| |
| public void setAddress(String address) { |
| this.address = address; |
| } |
| |
| public void testDemo() { |
| System.out.println(bname+"::"+bauthor+"::"+address); |
| } |
| |
| } |
| |
| # beans标签中添加如下 |
| xmlns:p="http://www.springframework.org/schema/p" |
| |
| # bean.xml中编写如下 |
| <bean id="book" class="com.ychen.spring.model.Book" p:bname="九阳神功" p:bauthor="无名氏"> |
| </bean> |
| |
| # 测试类 |
| @Test |
| public void testBook1() { |
| ApplicationContext context = |
| new ClassPathXmlApplicationContext("bean1.xml"); |
| Book book = context.getBean("book", Book.class); |
| System.out.println(book); |
| book.testDemo(); |
| } |
| |
| # 测试 |
| com.ychen.spring.model.Book@25be7b63 |
| 九阳神功::无名氏::null |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术