发表于: 2017-09-08 23:09:28

1 776


今天完成的事情:今天还是继续学习spring

1.学习绝对路径和相对路径

绝对路径类似于: H:\\mavenproject\\springdemo\\spring_mybatis_demo2\\src\\main\\resources\\
相对路径:IDEA的相对路径根目录是project的根文件夹,所以相对路径的填写应该是src/main/resources/bean.xml

假如路径用斜杠是双斜杠

如果是反斜杠是单斜杠

2.今天学习了<<SpringFrameWork Developer’s Guide>>这本书,实现了一个小实例,spring有了新的理解.

TestQuickStart测试类

public class TestQuickStart {
   
@Test
   
public void testQuickStart() {
        ApplicationContext ctx=
new
               
FileSystemXmlApplicationContext("src/main/resources/bean.xml");
       
Action action = (Action) ctx.getBean("TheAction");
       
System.out.println(action.execute("Rod Johnson"));
   
}
}

 

UpperAction implement

public class UpperAction implements Action {
   
private String message;
    public
String getMessage() {
       
return message;
   
}
   
public void setMessage(String string) {
       
message = string;
   
}
   
public String execute(String str) {
       
return (getMessage() + str).toUpperCase();
   
}
}

bean.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"
      
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <description>
Spring Quick Start</description>
   
<!--<bean id="TheAction"-->
          <!--class="com.mycom.myapp.dao.Impl.UpperAction">-->
   
<bean id="TheAction"
         
class="com.mycom.myapp.dao.Impl.LowerAction">
        <property
name="message">
            <value>
HeLLo</value>
        </property>
    </bean>
</beans>

Bean是什么

1. java面向对象,对象有属性和方法,那么就需要实例这些对象来调用这些属性及方法.

2. 凡是子类带有方法和属性的类都要加上注册beanspring ioc的注解

3. Bean其实就是类的代理及代言人,

spring的一点理解,spring的机制应该是先查找bean.xml文件,然后将其当成类,实例化一个对象,

ApplicationContext ctx=new FileSystemXmlApplicationContext("src/main/resources/bean.xml");

这个对象去调用springframework里的方法getbean.通过设置的id调出bean,

Action action = (Action) ctx.getBean("TheAction");

通过class找到需要替换设置的类.通过name找到需要修改的属性,未它赋上value.

<bean id="TheAction"
     
class="com.mycom.myapp.dao.Impl.LowerAction">
    <property
name="message">
        <value>
HeLLo</value>

 

好处:spring通过依赖注入,,将依赖关系从编码中脱离下来,不需要为了更改属性在编写一个类.降低了组件之间的耦合,提高重用性,实现组件上真正意义的即插即用

遇到问题:

1.因为本地仓库一直默认在cm2文件夹里,太占内存.所以想改地址.但是改了settings.xml文件和maven软件的settings,依然存到c.不知道什么原因.后来发现我的settings文件有问题.虽然我改了<localRepository>标签里内容它被注释了.然后换了师兄的settings文件添加依赖正常;

2.在添加log4j的依赖的时候,配置报错,后来发现是版本问题.正常流程应该是配置文件先到本地仓库里找,如果没有到ptteng私服上找.如果没找到本版本jar,就会报错.解决办法,将版本换成自己本地仓库jar包的版本.

3.mysql一直乱码,中文都是汉字,试了无数办法都没用,打算重装.

明天计划完成的事情;明天复习一下mybatis和jdbc

收获:


返回列表 返回列表
评论

    分享到