发表于: 2018-01-13 23:28:22
1 632
一。今天准备小课堂,想讲Spring的反转控制(IOC),和(DI)依赖注入。
一开始出的错误说是IOException parsing XML document from class path resource
意思是IO异常解析来自类路径资源的XML文档,意思也就是application.xml文件放错位置了。见图
把applicationContext放在resources下,终于可以了。Maven项目推荐各种资源文件都放在src/java/resources目录下,主要放一些和代码无关的配置,或者示例输入输出文件等。这些配置属于程序启动时必须读取的,比如spring的context等。
我们搭框架的时候,常常还有testresources配置,他们两个有什么区别呢?
达:他们两个有不同的输出路径,跑test时候test的路径优先级比较高,在testresources的classpath找到就不去resources的classpath找了。反之,跑正常程序找resources的文件,所以如果都有log4j的配置文件,跑test的会使用testresources的配置文件,直接跑src下的程序会使用resources下的配置文件。
二。还有一个是application文件本身的配置问题:一开始beans下面有红色曲线,还有几个http链接是无色的,也就是没有作用。我按住alt+enter键一调整,把几个链接调到最后一行了。运行,出现了如下错误:元素 'beans' 必须不含字符 [子级], 因为该类型的内容类型为“仅元素”。这句话是IDEA自己翻译的,并不通顺,百度一下,是bean错误,说明有不合理的输入出现了。于是我把http的那几句话重新调整了一下,运行,搞定了。如图:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean name="c" class="com.how2java.pojo.Category">
<property name="name" value="category 1" />
</bean>
</beans>
(applicationContext.xml是Spring的核心配置文件,通过关键字c即可获取Category对象,该对象获取的时候,即被注入了字符串"category 1“到name属性中)
在创建Product的时候注入一个Category对象
注意,这里要使用ref来注入另一个对象
<bean name="p" class="com.how2java.pojo.Product">
<property name="name" value="product1" />
<property name="category" ref="c" />
</bean>
运行,出现错误:
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'p' defined in class path resource [applicationContext.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'name' of bean class [com.how2java.pojo.Product]: Bean property 'name' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
百度一下原因是:applicationContext.xml中配置的bean属性‘p’中的set和get方法不匹配导致的,查看下这个实体类中的属性和数据库中的字段属性是不是对应的。
然后我们找到Product类。代码如下:
package com.how2java.pojo;
public class Product {
private int id;
private String name;
private Category category;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
}
发现括号里居然没有绿色字母,只是个空括号,怪不得我在applicationContext.XML里总是解决不了。
三。Spring注解,代码如下:
package com.how2java.pojo;
public class Product {
private int id;
private String name;
@Autowired //spring可以自动帮你把bean里面引用的对象的setter/getter方法省略,它会自动帮你set/get
private Category category;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
}
我加入了@Autowired注解,运行报错,说java找不到符号。我很奇怪,到底是哪里符号漏下了呢?
最后才发现,是包下面没有import org.springframework.beans.factory.annotation.Autowired;
import的作用是什么呢?当我们的类需要调用其他包内的类时,需要将目标类的包名导入,否则无法使用。
运行成功,见图,注解。
四。换一种注解方式,首先是Category
package com.how2java.pojo;
import org.springframework.stereotype.Component;@Component("c") // 为Category 类加上@Component注解,即表明此类是bean
public class Category {
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
private int id;
private String name="category 1";
}
然后是Product
package com.how2java.pojo;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;@Component("p") //为Product类加上@Component注解,即表明此类是bean
public class Product {
private int id;
private String name="product 1";
@Autowired
private Category category;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
}
Testspring不用动。,但是applicationContext要把最后改为一句话:
<context:component-scan base-package="com.how2java.pojo"/>
运行,成功。
明天的计划:继续Spring,把它当成小课堂
遇到的问题:暂无
今天的收获:对applicationContext配置加深了了解,还是各种Spring注解。想来以后的mybatis注解也会更好理解
java任务一开始时间:2017.12.05
预计demo时间:2018.01-05
可能有延期风险,原因是:已经延期了,基础比较差,
禅道链接地址:http://task.ptteng.com/zentao/project-task-501.html
评论