发表于: 2020-06-13 17:01:14
3 1708
今日作为:
弄清楚了昨天的一个问题
这个<property name = ""是什么意思
其实就是指的要被注入的那个类或方法里面的setter方法的名字。。。。。。。
因为我改了一下setter方法的名字XML里面会报错= =
(强转还是没怎么明白。。。明天再动手试试,只是记得书上说过类型转换,向下或者向上转型)
<bean id="vvv" class="com.jnshu.dao.UserServiceImpl">
<property name="userDao" ref="VVV"/>
</bean>
public void setUserDao(UserDao userDao){
this.userDao = userDao;
}
Spring作用域有什么用?
根据结果我发现singleton是不同的栈内存指向同一个堆内存
prototype是Java开辟了两个堆内存而且里面内容相同
但是这有什么作用么?
<bean id="scope1" class="com.jnshu.scope.Scope" scope="singleton"/>
<!--
<bean id="scope1" class="com.jnshu.scope.Scope" scope="prototype"/>
-->
public class ScopeTest {
@Test
public void scopeTest() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
System.out.println(applicationContext.getBean("scope1"));
System.out.println(applicationContext.getBean("scope1"));
}
}
结果
修改作用域
<!--<bean id="scope1" class="com.jnshu.scope.Scope" scope="singleton"/>-->
<bean id="scope1" class="com.jnshu.scope.Scope" scope="prototype"/>
结果
然后是Spring的
赋值注入和构造注入
public class User {
private String username;
private String password;
private List<String> list;
public User(String username, String password, List<String> list) {
super();
this.username = username;
this.password = password;
this.list = list;
}
@Override
public String toString() {
return "User{" +
"username='" + username + '\'' +
", password='" + password + '\'' +
", list=" + list +
'}';
}
public User(){
super();
}
public void setUsername(String username) {
this.username = username;
}
public void setPassword(String password) {
this.password = password;
}
public void setList(List<String> list) {
this.list = list;
}
}
applicationContext.xml
<bean id="user1" class="com.jnshu.assemble.User">
<property name="username" value="哈哈哈哈"></property>
<property name="password" value="7777"></property>
<property name="list">
<list>
<value>"listValue1"</value>
<value>"listValue2"</value>
</list>
</property>
</bean>
<bean id="user2" class="com.jnshu.assemble.User">
<constructor-arg index="0" value="咯咯咯咯"/>
<constructor-arg index="1" value="9999"/>
<constructor-arg index="2">
<list>
<value>"constructorValue1"</value>
<value>"constructorValue1</value>
</list>
</constructor-arg>
</bean>
但是就是很不理解赋值和构造注入有啥意义?一般不都是动态的用setter方法赋值然后结果给getter么?
需要初始化的话写在构造方法里面就好了呀?需要注入就在用setter方法了呀。。。
然后学习了Spring Annotation
感觉懂了,又感觉没懂。。。你让我问问题我也问不出来,明天上午争取搞懂吧= =
还一个就是beans.xml我丢在annotation文件夹下在程序里面写好了路径但是就是找不着
放在resouces文件夹里面就可以
请教一下师兄有啥办法么?
明天计划:1.搞懂Spring Annotation
2.搞懂Spring AOP
3.搞懂Spring 事务管理
全部继续动手做实验。。。。。
评论