java的Spring

时间:2023-03-09 14:56:00
java的Spring

     歇了一年都没有写过自己博客了,在学习新东西的时候 ,应该把它们记下来,学了.net 去了公司没有多久就转成了java虽然都在做,还是觉得.net好,不过东西还是应该学习下去,这样才是正解!

首先学习的Spring ,在网上找了哈视频 以及查阅了资料,据说Spring 是一个容器,我觉得它就是不用new对象 ,就可以实现对象操作!当然它肯定还有其它的功能 ,我刚刚学习了一一首先应该是研究它 配置文件:

<?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-2.5.xsd">
<bean id="personService" class="cn.wdkj.spring.testSpring" scope="prototype"></bean>
</beans>

这里bean 节点下面的perspnService这个东西意思,它Spring 实例化对应到是那个架包下面的那一个类,这里对应的是cn.wdkj.spring.testSpring 这个数,Scope表示是不是每次是实例化个一个新的东西,如果scope="prototype" 表示每次调用都会产生一个新的实例,如果 不写说明没有调用新的实例,还是原来那个

调用代码:

@Test
public void test() {
ApplicationContext cxt = new ClassPathXmlApplicationContext ("beans.xml");
testSpringServices test=(testSpringServices)cxt.getBean("personService");
testSpringServices test2=(testSpringServices)cxt.getBean("personService");
System.out.println(test==test2);
test.SayHello();
}

ApplicationContext 这个是Spring 取特定取到对象类,它是读以xml所有bean这个节点的信息,我觉得它应该返回的是一个集合对象,然后根据bean节点的Id,找到具体是那个bean ,然后得到那上bean 然后得到这个类,然而返回的是这个类的继承的接口,这样更好的封装!
      System.out.println(test==test2);//判断两个对象是不是相等,使用==号,这样比较是值相等和引用类型也相等!
   今天暂时写到这里,每天进步!