public class Test { public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); People people = ac.getBean("peo",People.class); System.out.println(people); String[] names = ac.getBeanDefinitionNames(); for (String string : names) { System.out.println(string); } } }
Spring创建对象的三种方式
1. 构造方法创建
无参构造创建:默认
有参构造创建:需要明确配置
2. 实例工厂
3. 静态工厂
构造方法创建(bean标签更改)
实体类创建好无参构造方法和有参构造方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
public People(){ super(); System.out.println("执行无参构造方法"); }
public People(int id,String name){ super(); this.id=id; this.name=name; System.out.println("执行有参构造方法1"); }
public People(String name,int id){ super(); this.id=id; this.name=name; System.out.println("执行有参构造方法2"); }
<select id="selAll1" resultMap="mymap1"> select t.id tid,t.name tname,s.id sid,s.name sname,age,tid from teacher t LEFT JOIN student s on t.id=s.tid; </select> </mapper>
//只列举了变量 private int id; private String name; private int age; private int tid; private Teacher teacher; //老师类的对象!!!!!!
2. teacher配置文件(一个是student)
1 2 3 4 5 6
<mapper namespace="com.bjsxt.mapper.TeacherMapper"> <select id="selAll" resultType="teacher"> select t.id id,t.name name,s.id `list.id`,s.name `list.name`,age `list.age`,tid `list.tid` from teacher t LEFT JOIN student s on t.id=s.tid //展示的是list集合输出 </select> </mapper>
3. student配置文件(里面通过老师对象(反单引号) teacher.id等传过来用)
1 2 3 4 5 6 7
<mapper namespace="com.bjsxt.mapper.StudentMapper"> <select id="selAll" resultType="student"> select t.id `teacher.id`,t.name `teacher.name`,s.id id,s.name name,age,tid from student s LEFT JOIN teacher t on t.id=s.tid //里面通过老师对象(反单引号) teacher.id等传过来用) </select> </mapper>
public class Test { public static void main(String[] args) { //使用ThreadLocal类 final ThreadLocal<String> threadLocal=new ThreadLocal<>(); //final不让实例化 threadLocal.set("测试"); //创建子线程 new Thread() { public void run() //匿名内部类 { String result = threadLocal.get(); System.out.println("结果"+result); }; }.start(); String result = threadLocal.get(); System.out.println(result); } }