智者不为愚者谋,勇者不为怯者死。这篇文章主要讲述Spring的applicationContext默认使用无参构造创建对象相关的知识,希望能为你提供帮助。
1 public class User { 2private String name; 3 4public User() { 5System.out.println("User的无参构造"); 6} 7 8public String getName() { 9return name; 10} 11 12public void setName(String name) { 13this.name = name; 14} 15 16 }
1 < ?xml version="1.0" encoding="UTF-8"?> 2 < beans xmlns="http://www.springframework.org/schema/beans" 3xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4xsi:schemaLocation="http://www.springframework.org/schema/beans 5https://www.springframework.org/schema/beans/spring-beans.xsd"> 6< bean id="user" class="com.rzk.pojo.User"> 7< property name="name" value="https://www.songbingjia.com/android/小明"/> 默认使用无参设置值 8< /bean> 9 10 11 < /beans>
1 public class Mytext { 2public static void main(String[] args) { 3ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); 4User user = (User) context.getBean("user"); 5< !--System.out.println(user.getName()); --> 6application一注册就实例化 7} 8 }
当我们创建ApplicationContext对象的时候 同时会将 配置文件中配置的bean对象一起创建
(饿汉模式 )这时候无参构造也被加载
文章图片
上面是使用无参设置值
-------------
下面是有参设置
1 public class User { 2private String name; 3 4public User(String name) { 5this.name = name; 6System.out.println("User的有参构造"); 7} 8 9public String getName() { 10return name; 11} 12 13public void setName(String name) { 14this.name = name; 15} 16 17 }
直接通过参数名设置值
1< ?xml version="1.0" encoding="UTF-8"?> 2< beans xmlns="http://www.springframework.org/schema/beans" 3xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4xsi:schemaLocation="http://www.springframework.org/schema/beans 5https://www.springframework.org/schema/beans/spring-beans.xsd"> 6< bean id="user" class="com.rzk.pojo.User"> 7< !--< property name="name" value="https://www.songbingjia.com/android/小明"/> --> 8< constructor-arg name="name"value="https://www.songbingjia.com/android/小明" /> 使用有参注入值 9< /bean> 10< /beans>
-------------
通过下标赋值
1 public class User { 2private String name; 3private int age; 4 5public User(String name, int age) { 6this.name = name; 7this.age = age; 8System.out.println("User的有参构造"); 9} 10 11public String getName() { 12return name; 13} 14 15public void setName(String name) { 16this.name = name; 17} 18 19public int getAge() { 20return age; 21} 22 23public void setAge(int age) { 24this.age = age; 25} 26 }
1 < ?xml version="1.0" encoding="UTF-8"?> 2 < beans xmlns="http://www.springframework.org/schema/beans" 3xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4xsi:schemaLocation="http://www.springframework.org/schema/beans 5https://www.springframework.org/schema/beans/spring-beans.xsd"> 6< bean id="user" class="com.rzk.pojo.User"> 7< constructor-arg index="0"value="https://www.songbingjia.com/android/小明" /> 8< constructor-arg index="1" value="https://www.songbingjia.com/android/12"/> 9< /bean> 10 < /beans>
【Spring的applicationContext默认使用无参构造创建对象】
推荐阅读
- ApplicationConsul安装及运行
- application.properties中的list配置
- Android 架构组件 之 ViewModel 学习
- Android android problem
- Reface.AppStarter 框架初探
- Android应用加固的简单实现方案
- wake up on lan 安卓手机控制局域网唤醒开机+远程关机
- flask 源码专题(app.run()的背后)
- 安卓计步器的简单实现