发表于: 2017-08-26 23:55:49
1 883
今天完成的任务
学习了Spring
1)和mybatis一样创建一个Category类
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;
}
2)编写xml文件
<?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>
在此
1.xmlns:关于初始化bean的格式文件地址
2.xmlns:xsi:辅助初始化bean
3.xsi:context:关于spring上下文,包括加载资源文件
4.xsi:schemaLocation:用于声明了目标名称空间的模式文档
但具体里面写的是啥我百度半天还是不清楚
<bean name="c" class="com.how2java.pojo.Category">
<property name="name" value="category 1" />
</bean>
name:确定该Bean的唯一标识符,容器对Bean管理、访问、以及该Bean的依赖关系,都通过该属性完成。Bean的id属性在Spring容器中是唯一的
class:指定该Bean的具体实现类。注意这里不能使接口。通常情况下,Spring会直接使用new关键字创建该Bean的实例,因此,这里必须提供Bean实现类的类名。
property:依赖注入,配置当前类中相应的属性 ,据说有三种注入,我这边教程只用了这一种
3)TestSpring主方法
和mybatis一样,主方法大体类似
public class TestSpring {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationContext.xml" });
Category c = (Category) context.getBean("c");
//获取到name为c的bean标签里面的内容强转为category类型
System.out.println(c.getName());
}
明天要完成的任务
接着深入Spring
遇到的问题
1.xml输入的这些地址到底是什么,我看的一脸懵
收获
依然没啥
评论