发表于: 2018-01-11 02:11:29
1 619
今天完成
1.依赖注入
每个基于应用程序的 java 都有几个对象,这些对象一起工作来呈现出终端用户所看到的工作的应用程序。当编写一个复杂的 Java 应用程序时,应用程序类应该尽可能独立于其他 Java 类来增加这些类重用的可能性,并且在做单元测试时,测试独立于其他类的独立性。依赖注入(或有时称为布线)有助于把这些类粘合在一起,同时保持他们独立。
java中属性注入的方式:set()方法注入、有参构造注入、接口注入;
spring框架中属性注入的方式:set()方法注入、有参构造注入;
package com.test;
public class test1 {
private String username;
public test1(String username ){
this.username=username;
}
public void test2(){
System.out.println("name:"+username);
}
}
package com.test;
import com.test.test1;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp3 {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("Beans3.xml");
test1 te = (test1) context.getBean("dex");
te.test2();
}
}
<?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="dex" class="com.test.test1">
<constructor-arg name="username" value="zhangsan"/>
</bean>
</beans>
2.线下报名算吗??
明天计划
1.完成spring依赖注入部分,找更多的实例进行学习,加深印象;
2.学习并尝试写spring JDBC;
遇到问题
开始对依赖注入概念不清楚,注入是什么操作,要达到什么目的。快速看了一些讲解后才有了个清晰的概念;
收获
学习了依赖注入,学到了有参构造注入,粗略看了set方法
评论