发表于: 2018-01-12 01:58:42

1 621


今天完成

  1.学习依赖注入:

   1).有参构造注入不同类型的数据:

<?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="test1" class="com.test.test1">
       <constructor-arg type="java.lang.String" value="ZHANGSAN"/>
       <constructor-arg type="int" value="33"/>
       </bean>

       </beans>

    输出:name:ZHANGSAN  age:33

<?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="test1" class="com.test.test1">
   <constructor-arg index="0" value="lisi"/>
   <constructor-arg  index="1" value="22"/>
</bean>

</beans>

    输出:name:lisi  age:22

  向一个对象传递一个引用,需要使用 标签的 ref 属性,如果你想要直接传递值,那么你应该使用如上所示的 value 属性。

   2).set()方法进行注入

     今天没有写实例练习,明天补上;

2.学习java类的加载顺序

package com.tutorialspoint2;

public class TextEditor {
private SpellChecker spellChecker;
   public TextEditor(SpellChecker spellChecker) {
System.out.println("Inside TextEditor constructor." );

       this.spellChecker = spellChecker;
   }
public void spellCheck() {
spellChecker.checkSpelling();

   }
}
package com.tutorialspoint2;
//依赖类
public class SpellChecker {
public SpellChecker(){

System.out.println("pingxie检查 编辑器" );
   }
public void checkSpelling() {
System.out.println("拼写检查中." );

}

package com.tutorialspoint2;
//测试类
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp2 {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("Bean2.xml");
       TextEditor te = (TextEditor) context.getBean("textEditor");
       te.spellCheck();
   }
}
<?xml version="1.0" encoding="UTF-8"?>//xml文件
<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">

   <!-- Definition for textEditor bean -->
   <bean id="textEditor" class="com.tutorialspoint2.TextEditor">
       <constructor-arg ref="spellChecker"/>
   </bean>

   <!-- Definition for spellChecker bean -->
   <bean id="spellChecker" class="com.tutorialspoint2.SpellChecker"/>
</beans>

  输出:pingxie检查 编辑器

        Inside TextEditor constructor.

        拼写检查中.

     这里看不懂为什么会输出这些内容,就上网查询java的加载顺序:父类的静态方法>父类静态初始化块>子类静态初始化块>父类非静态初始化块>父类的构造方法>子类非静态初始化块>子类的构造方法

       

     由此分析上面的代码: 由主方法(测试类)中“te.spellCheck();  ”这一句代码决定了先加载TextEditor类>加载TextEditor类中的成员变量private SpellChecker spellChecker;>加载SpellChecker类>加载SpellChecker类中的构造方法,输出“pingxie检查 编辑器”>加载TextEditor类中的构造方法,输出“Inside TextEditor constructor.”>执行te.spellCheck();  ”,输出拼写检查中.  ”;


3.Spring 注入内部 Beans

     和内部类的定义相似,inner beans 是在其他 bean 的范围内定义的 bean;

4.Spring 注入集合

     spring理由set()方法注入集合(<list>、<set>、<map>、<props>四种集合

5.Spring JDBC 框架初步学习

    JdbcTemplate 对JDBC进行了封装

明天计划

1. 学习JdbcTemplate ;

遇到问题

——

收获

——


返回列表 返回列表
评论

    分享到