随记录 之 Spring 自学笔记(一) -- IOC(一)

来源:互联网 发布:创业公司 程序员 编辑:程序博客网 时间:2024/05/16 17:36


Spring 自学


首先一个简单DEMO:


POJO:

public class Student {private String number;private String name;private int age;public Student(String name, String number, int age){this.age = age;this.name = name;this.number = number;}
}



1.IOC


注入:

1、构造器注入

2、setter注入


1》构造器注入



<!-- 构造器 注入 
1、使用 type 属性显式的指定了构造函数参数的类型 
2、 使用 index 属性来显式的指定构造函数参数的索引 -->

<bean id="student10" class="com.wm.spring.IOC.Student" scope="prototype"><constructor-arg type="java.lang.String" value="Tom" /><constructor-arg type="java.lang.String" value="20130513" /><constructor-arg type="int" value="19" /></bean><bean id="student11" class="com.wm.spring.IOC.Student" scope="prototype"><constructor-arg index="0" value="Tom" /><constructor-arg index="1" value="20130513" /><constructor-arg index="2" value="19" /></bean>

这儿测试的时候,如果定义两个student,scope没有写成prototype会报错,这是因为默认是singleton单例,只能有一个class一样的,id会失效


2》setter注入

<bean id="student" class="com.wm.spring.IOC.Student" scope="prototype"><property name="number" value="20130512"/><property name="name" value="jack"/><property name="age" value="18"/></bean>

        // setter 注入时 必须要有一个无参构造函数public Student(){System.out.println("Student 默认构造器 。。。。。");}

在使用setter注入时,如果有如下报错,则是因为POJO没有 无参构造函数
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'student' defined in class path resource [spring.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.wm.spring.IOC.Student]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.wm.spring.IOC.Student.<init>()at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1076)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1021)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:323)at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:973)at com.wm.spring.TEST.MainTest.TestBean(MainTest.java:26)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)at java.lang.reflect.Method.invoke(Method.java:606)at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:69)at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:48)at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)at org.junit.runners.ParentRunner.run(ParentRunner.java:292)at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.wm.spring.IOC.Student]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.wm.spring.IOC.Student.<init>()at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:85)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1069)... 31 moreCaused by: java.lang.NoSuchMethodException: com.wm.spring.IOC.Student.<init>()at java.lang.Class.getConstructor0(Class.java:2849)at java.lang.Class.getDeclaredConstructor(Class.java:2053)at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:80)... 32 more


2.bean作用范围 

--scope  默认作用域是单例

bean支持的5种范围域:
singleton   单例   - 每个Spring IoC 容器返回一个bean实例
prototype        原型   - 当每次请求时返回一个新的bean实例
request          请求   - 返回每个HTTP请求的一个Bean实例
session     会话   - 返回每个HTTP会话的一个bean实例
          全局会话- 返回全局HTTP会话的一个bean实例

     
     
     

    测试例子:

public class MainTest {private ApplicationContext ac = null;{ac = new ClassPathXmlApplicationContext("spring.xml");}@Testpublic void TestBean(){System.out.println("setter 注入:");Student student = (Student)ac.getBean("student");String result = student.toString();System.out.println(result);}@Testpublic void TestConstructor(){System.out.println("构造器注入:");System.out.println("使用type:");Student student = (Student)ac.getBean("student10");System.out.println(student.toString());System.out.println("使用index:");Student student1 = (Student)ac.getBean("student11");System.out.println(student1.toString());}}



maven 的 pom.xml配置文件:

<!-- spring --><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>4.0.2.RELEASE</version></dependency><!-- test --><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.9</version><scope>test</scope></dependency>



     
      
0 0
原创粉丝点击