测试:java
实体类面试
public class User { private String name; private Integer age; private Date date; public User(){ } public User(String name, Integer age, Date date) { this.name = name; this.age = age; this.date = date; } @Override public String toString() { return "User{" + "name='" + name + '\'' + ", age=" + age + ", date=" + date + '}'; } }
Spring核心配置文件beans.xml算法
<bean id="user" class="com.Dao.User"> <constructor-arg name="name" value="chenhui"></constructor-arg> <constructor-arg name="age" value="22"></constructor-arg> <constructor-arg name="date" ref="date"></constructor-arg> </bean> <bean id="date" class="java.util.Date"></bean>
测试类:spring
@Test public void test(){ //经过ClassPathXmlApplicationContext对象加载配置文件方式将javabean对象交给spring来管理 ApplicationContext Context=new ClassPathXmlApplicationContext("bean.xml"); //获取Spring容器中的bean对象,经过id和类字节码来获取 User user = Context.getBean("user", User.class); System.out.println(user); }
结果:数据库
要求被注入的属性 , 必须有set方法 , set方法的方法名由set + 属性首字母大写 , 若是属性是boolean类型 , 没有set方法 , 是 is .设计模式
测试:缓存
实体类数据结构
public class User { private String name; private Integer age; private Date date; public User(){ } @Override public String toString() { return "User{" + "name='" + name + '\'' + ", age=" + age + ", date=" + date + '}'; } public void setName(String name) { this.name = name; } public void setAge(Integer age) { this.age = age; } public void setDate(Date date) { this.date = date; } }
Spring核心配置文件beans.xml多线程
<!-- 使用的是默认构造函数建立对象--> <bean id="user" class="com.Dao.User"> <property name="name" value="chenhui"></property> <property name="age" value="22"></property> <property name="date" ref="date"></property> </bean> <bean id="date" class="java.util.Date"></bean>
测试类:并发
@Test public void test(){ //经过ClassPathXmlApplicationContext对象加载配置文件方式将javabean对象交给spring来管理 ApplicationContext Context=new ClassPathXmlApplicationContext("bean.xml"); //获取Spring容器中的bean对象,经过id和类字节码来获取 User user = Context.getBean("user", User.class); System.out.println(user); }
结果:
复杂类型/集合类型的注入
测试:
实体类:
public class User { private String[] strings; private List<String> list; private Map map; private Properties properties; private Set set; public void setStrings(String[] strings) { this.strings = strings; } public void setList(List<String> list) { this.list = list; } public void setMap(Map map) { this.map = map; } public void setProperties(Properties properties) { this.properties = properties; } public void setSet(Set set) { this.set = set; } public User(){ } public void print(){ System.out.println(Arrays.toString(strings)); System.out.println(list); System.out.println(map); System.out.println(set); System.out.println(properties); } }
Spring核心配置文件beans.xml
<bean id="user" class="com.Dao.User"> <property name="strings"> <array> <value>dasd</value> <value>aaaa</value> <value>dfff</value> </array> </property> <property name="list"> <list> <value>dff</value> <value>hjjgj</value> <value>dgfhfgh</value> </list> </property> <property name="set"> <set> <value>1213</value> <value>1fsdf</value> <value>trg</value> </set> </property> <property name="map"> <map> <!-- 使用第一种方式--> <entry key="chenhui" value="123"></entry> <entry key="xie" value="123"></entry> <!-- 也可以使用第二种方式--> <entry key="chen"> <value>333</value> </entry> </map> </property> <property name="properties"> <props> <prop key="cc">1</prop> <prop key="ww">2</prop> <prop key="dsa">3</prop> </props> </property> </bean>
测试类:
@Test public void test(){ //经过ClassPathXmlApplicationContext对象加载配置文件方式将javabean对象交给spring来管理 ApplicationContext Context=new ClassPathXmlApplicationContext("bean.xml"); //获取Spring容器中的bean对象,经过id和类字节码来获取 User user = Context.getBean("user", User.class); //调用方法使集合类型注入执行 user.print(); }
结果:
当结构相同,标签能够互换
<bean id="user" class="com.Dao.User"> <property name="strings"> <set> <value>1213</value> <value>1fsdf</value> <value>trg</value> </set> </property> <property name="list"> <array> <value>dasd</value> <value>aaaa</value> <value>dfff</value> </array> </property> <property name="set"> <list> <value>dff</value> <value>hjjgj</value> <value>dgfhfgh</value> </list> </property> <property name="map"> <props> <prop key="cc">1</prop> <prop key="ww">2</prop> <prop key="dsa">3</prop> </props> </property> <property name="properties"> <map> <!-- 使用第一种方式--> <entry key="chenhui" value="123"></entry> <entry key="xie" value="123"></entry> <!-- 也可以使用第二种方式--> <entry key="chen"> <value>333</value> </entry> </map> </property> </bean>
结果:
[图片上传中...(image-85fd26-1613912046687-0)]
欢迎关注公众号:前程有光,领取一线大厂Java面试题总结+各知识点学习思惟导+一份300页pdf文档的Java核心知识点总结! 这些资料的内容都是面试时面试官必问的知识点,篇章包括了不少知识点,其中包括了有基础知识、Java集合、JVM、多线程并发、spring原理、微服务、Netty 与RPC 、Kafka、日记、设计模式、Java算法、数据库、Zookeeper、分布式缓存、数据结构等等。