[关闭]
@EggGump 2018-03-05T15:53:41.000000Z 字数 10371 阅读 440

Spring第一个 hello

未分类


创建一个javaproject learnSpring
创建一个lib文件夹,commons-logging.jar和spring.jar放入里面,并add to build path
在learnSpring的properties中,java Build Path/Libraries/add Library/Junit/Junit4
创建包com.neu.hello
创建类com.neu.hello.HelloWorld.java

  1. package com.neu.hello;
  2. public class HelloWorld {
  3. public void hello() {
  4. System.out.println("Hello") ;
  5. }
  6. }

创建applicationContext.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
  6. <bean id = "helloWorld" class = "com.neu.hello.HelloWorld"></bean>
  7. </beans>

创建包com.neu.test
创建类com.neu.test.HelloTest.java

  1. package com.neu.test;
  2. import org.junit.Test;
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;
  5. import com.neu.hello.HelloWorld;
  6. public class HelloTest {
  7. @Test
  8. public void testHello() {
  9. ApplicationContext context =
  10. new ClassPathXmlApplicationContext("applicationContext.xml") ;
  11. HelloWorld hello = (HelloWorld)context.getBean("helloWorld") ;
  12. hello.hello();
  13. }
  14. }

右击 run as / JUnit Test

别名:xml文件里加:

  1. <alias name="helloWorld" alias="shit"/> //name要和类的id一致

这样,便可利用 context.getBean("shit")来获得一个新象

spring默认构造对象是用无参的构造函数进行。
静态工厂创建对象
先创建类

  1. package com.neu.hello;
  2. public class HelloWorldFactory {
  3. public static HelloWorld getInstance() {
  4. return new HelloWorld() ;
  5. }
  6. }

再在xml中添加如下内容

  1. <bean id = "helloWorld2" class = "com.neu.hello.HelloWorldFactory" factory-method="getInstance"></bean>

最后在客户端这样写就可以了:

  1. HelloWorld hello = (HelloWorld)context.getBean("helloWorld2") ;

实例工厂创建对象
xml文件添加:

  1. <!-- 实例工厂
  2. 获得类的方法不能是静态的,否则出错
  3. -->
  4. <bean id = "helloWorldFactory" class = "com.neu.hello.HelloWorldFactory"></bean>
  5. <bean id = "helloWorld3" factory-bean="helloWorldFactory" factory-method="getInstance2"></bean>

工厂类里:

  1. package com.neu.hello;
  2. public class HelloWorldFactory {
  3. public HelloWorld getInstance2() {
  4. return new HelloWorld() ;
  5. }
  6. }

测试类:

  1. HelloWorld hello = (HelloWorld)context.getBean("helloWorld3");
  2. hello.hello();
  • 第一种默认情况下,spring容器产生的对象是单例的,也就是说,context.getBean("xxx")执行多次,也只会产生一个对象。
  1. <bean id = "helloWorld" class = "com.neu.hello.HelloWorld" scope ="prototype"></bean>

多添加一个scope便可让helloWorld产生多例对象,此时,在getBean时会创建对象。默认只会在spring容器初始化时创建对象,当lazy-init="true"时,便会在调用getBean(x)时产生对象,当有scope时,lazy-init失效

  • init-method和destroy-method
    xml文件中添加参数
  1. <bean id = "helloWorld" class = "com.neu.hello.HelloWorld" scope="prototype"
  2. init-method="init" destroy-method="destroy"></bean>

HelloWorld文件中添加方法

  1. package com.neu.hello;
  2. public class HelloWorld {
  3. public HelloWorld() {
  4. System.out.println("new instance") ;
  5. }
  6. public void init() {
  7. System.out.println("init") ;
  8. }
  9. public void destroy() {
  10. System.out.println("destroy");
  11. }
  12. public void hello() {
  13. System.out.println("Hello") ;
  14. }
  15. }

spring在创建对象时,会调用init方法,在容器销毁时会调用destroy方法测试代码:这里需要强制转换才能关闭容器。如果有scope=prototype,那么不负责销毁。

  1. public void test() {
  2. ApplicationContext context =
  3. new ClassPathXmlApplicationContext("applicationContext.xml") ;
  4. HelloWorld hello = (HelloWorld)context.getBean("helloWorld") ;
  5. hello.hello();
  6. ClassPathXmlApplicationContext applicationContext =
  7. (ClassPathXmlApplicationContext)context ;
  8. applicationContext.close();
  9. }

依赖注入 DI

set方法注入 必须要有默认构造函数

类文件Person.java 这里的set 和 get方法一定要按格式写,否则后面xml文件写时会出错

  1. import java.util.Arrays;
  2. import java.util.List;
  3. import java.util.Map;
  4. import java.util.Properties;
  5. import java.util.Set;
  6. public class Person {
  7. private Long pid;
  8. private String name;
  9. private Student student;
  10. private List lists;
  11. private Set sets;
  12. private Map map;
  13. private Object[] objects;
  14. private Properties properties;
  15. public Person(){
  16. System.out.println("person");
  17. }
  18. public Long getPid() {
  19. return pid;
  20. }
  21. public void setPid(Long pid) {
  22. this.pid = pid;
  23. }
  24. public String getName() {
  25. return name;
  26. }
  27. public void setName(String name) {
  28. this.name = name;
  29. }
  30. public Student getStudent() {
  31. return student;
  32. }
  33. public void setStudent(Student student) {
  34. this.student = student;
  35. }
  36. public List getLists() {
  37. return lists;
  38. }
  39. public void setLists(List lists) {
  40. this.lists = lists;
  41. }
  42. public Set getSets() {
  43. return sets;
  44. }
  45. public void setSets(Set sets) {
  46. this.sets = sets;
  47. }
  48. public Map getMap() {
  49. return map;
  50. }
  51. public void setMap(Map map) {
  52. this.map = map;
  53. }
  54. public Object[] getObjects() {
  55. return objects;
  56. }
  57. public void setObjects(Object[] objects) {
  58. this.objects = objects;
  59. }
  60. public Properties getProperties() {
  61. return properties;
  62. }
  63. public void setProperties(Properties properties) {
  64. this.properties = properties;
  65. }
  66. @Override
  67. public String toString() {
  68. return "Person [pid=" + pid + ", name=" + name + ", student=" + student
  69. + ", lists=" + lists + ", sets=" + sets + ", map=" + map
  70. + ", objects=" + Arrays.toString(objects) + ", properties="
  71. + properties + "]";
  72. }
  73. }

文件Student.java

  1. public class Student {
  2. public Student(){
  3. System.out.println("student");
  4. }
  5. public void say(){
  6. System.out.println("student");
  7. }
  8. }

xml文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
  6. <bean id="person" class="com.itheima11.spring.di.xml.setter.Person">
  7. <!--
  8. property描述的就是bean中的属性
  9. name属性就是描述属性的名称
  10. value就是值 如果是基本属性(String),就用value赋值
  11. ref 如果是引用类型,用ref赋值
  12. -->
  13. <property name="pid" value="1"></property>
  14. <property name="name" value="王二麻子"></property>
  15. <property name="student" ref="student"></property>
  16. <property name="lists">
  17. <list>
  18. <value>list1</value>
  19. <value>list2</value>
  20. <!--
  21. list中存放一个student对象
  22. -->
  23. <ref bean="student"/>
  24. </list>
  25. </property>
  26. <property name="objects">
  27. <list>
  28. <value>obj1</value>
  29. <ref bean="student"/>
  30. </list>
  31. </property>
  32. <property name="sets">
  33. <set>
  34. <value>set1</value>
  35. <ref bean="student"/>
  36. </set>
  37. </property>
  38. <property name="map">
  39. <map>
  40. <entry key="m1">
  41. <value>m1</value>
  42. </entry>
  43. <entry key="m2">
  44. <ref bean="student"/>
  45. </entry>
  46. </map>
  47. </property>
  48. <property name="properties">
  49. <props>
  50. <prop key="p1">p1</prop>
  51. <prop key="p2">p2</prop>
  52. </props>
  53. </property>
  54. </bean>
  55. <bean id="student" class="com.itheima11.spring.di.xml.setter.Student"
  56. scope="prototype"></bean>
  57. </beans>

test.java

  1. public class DIXMLSetterTest {
  2. /**
  3. * 1、启动spring容器
  4. * 2、给person创建对象
  5. * 3、给student创建对象
  6. * 4、调用person的各个属性的setter方法赋值
  7. * 5、context.getBean
  8. * 6、对象调用方法
  9. */
  10. @Test
  11. public void testDI_XML_Setter_Default(){
  12. ApplicationContext context =
  13. new ClassPathXmlApplicationContext("applicationContext.xml");
  14. Person person = (Person)context.getBean("person");
  15. System.out.println(person.toString());
  16. }
  17. @Test
  18. public void testDI_XML_Setter_Person_Default_Student_Lazyinit_TRUE(){
  19. ApplicationContext context =
  20. new ClassPathXmlApplicationContext("applicationContext.xml");
  21. Person person = (Person)context.getBean("person");
  22. System.out.println(person.toString());
  23. }
  24. /**
  25. * 1、启动spring容器
  26. * 2、创建Student对象
  27. * 3、context.getBean
  28. * 4、创建person对象
  29. * 5、调用setter方法赋值
  30. * 6、对象调用方法
  31. */
  32. @Test
  33. public void testDI_XML_Setter_Person_Lazyinit_TRUE_Student_Lazyinit_Default(){
  34. ApplicationContext context =
  35. new ClassPathXmlApplicationContext("applicationContext.xml");
  36. Person person = (Person)context.getBean("person");
  37. System.out.println(person.toString());
  38. }
  39. /**
  40. * 1、启动spring容器
  41. * 2、创建Student对象
  42. * 3、context.getBean
  43. * 4、创建person对象
  44. * 5、调用setter方法赋值
  45. * 6、对象调用方法
  46. */
  47. @Test
  48. public void testDI_XML_Setter_Person_Scope_Prototype_Student_Scope_Default(){
  49. ApplicationContext context =
  50. new ClassPathXmlApplicationContext("applicationContext.xml");
  51. Person person = (Person)context.getBean("person");
  52. System.out.println(person.toString());
  53. }
  54. /**
  55. * 1、启动spring容器
  56. * 2、创建person对象
  57. * 3、创建student对象 student的scope为"prototype",但是创建对象在spring容器启动的时候
  58. * 因为Person中的很多属性都依赖于student,而这些属性的赋值发生在spring容器启动的时候
  59. * 4、调用setter方法赋值
  60. * 5、 context.getBean
  61. * 6、对象调用方法
  62. */
  63. @Test
  64. public void testDI_XML_Setter_Person_Scope_Default_Student_Scope_Prototype(){
  65. ApplicationContext context =
  66. new ClassPathXmlApplicationContext("applicationContext.xml");
  67. Person person = (Person)context.getBean("person");
  68. System.out.println(person.toString());
  69. }
  70. }

所有的scope和lazy-init都默认情况下,如果Person有init-method方法,那么选执行完所有的set方法进行装配,再调用init方法。

构造函数注入

Person.java

  1. package com.neu.hello;
  2. import java.util.* ;
  3. public class Person {
  4. private Long pid ;
  5. private String name ;
  6. private Student student ;
  7. public Person(Long pid, String name, Student student) {
  8. super();
  9. this.pid = pid;
  10. this.name = name;
  11. this.student = student ;
  12. }
  13. @Override
  14. public String toString() {
  15. return "Person [pid=" + pid + ", name=" + name + ", student=" + student + "]";
  16. }
  17. }

Student.java ,Test.java同上
xml文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
  6. <bean id = "person" class = "com.neu.hello.Person">
  7. <constructor-arg index="0" value="1"></constructor-arg>
  8. <constructor-arg index = "1" value="shit"></constructor-arg>
  9. <constructor-arg index = "2" ref="student"></constructor-arg>
  10. </bean>
  11. <bean id = "student" class = "com.neu.hello.Student"></bean>
  12. </beans>

注意,引用类型的使用ref

注解

今天学了一个新玩意儿
先创建两个注解 annotation
ClassInfo.java

  1. package com.neu.annotation;
  2. import java.lang.annotation.ElementType;
  3. import java.lang.annotation.Retention;
  4. import java.lang.annotation.RetentionPolicy;
  5. import java.lang.annotation.Target;
  6. @Target(ElementType.TYPE)//该注解能作用于class等上
  7. @Retention(RetentionPolicy.RUNTIME)
  8. public @interface ClassInfo {
  9. String name() default "" ;
  10. }

MethodInfo.java

  1. package com.neu.annotation;
  2. import java.lang.annotation.ElementType;
  3. import java.lang.annotation.Retention;
  4. import java.lang.annotation.RetentionPolicy;
  5. import java.lang.annotation.Target;
  6. @Target(ElementType.METHOD) //该注解能作用于方法
  7. @Retention(RetentionPolicy.RUNTIME)
  8. public @interface MethodInfo {
  9. String name() default "" ;
  10. }

再创建一个类,将刚才创建的注解用上
Cloud.java

  1. package com.neu.annotation;
  2. @ClassInfo(name = "11")
  3. public class Cloud {
  4. @MethodInfo(name = "learn java")
  5. public void java() {
  6. }
  7. }

最后再创建一个文件,通过反射将刚才的注解显示出来
AnnotationParse.java

  1. package com.neu.annotation;
  2. import java.lang.reflect.Method;
  3. import org.junit.Test;
  4. public class AnnotationParse {
  5. public static void parse() {
  6. Class class1 = Cloud.class ;
  7. //判断该类上有没有ClassInfo注解
  8. if(class1.isAnnotationPresent(ClassInfo.class)) {
  9. ClassInfo classInfo = (ClassInfo)class1.getAnnotation(ClassInfo.class) ;
  10. System.out.println(classInfo.name());
  11. }
  12. Method[] methods = class1.getMethods() ;
  13. for(Method method : methods) {
  14. //判断当前正在遍历的方法上是否有注解
  15. if(method.isAnnotationPresent(MethodInfo.class)) {
  16. MethodInfo methodInfo = method.getAnnotation(MethodInfo.class) ;
  17. System.out.println(methodInfo.name());
  18. }
  19. }
  20. }
  21. @Test
  22. public void Test() {
  23. AnnotationParse.parse();
  24. }
  25. }

注解注入

还是spring注入的一种方式,所以要按前面所说的spring方式创建java工程,这里,xml的命名空间要做一些改变,改变如下:
xml文件与原来相比多了几行

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:context="http://www.springframework.org/schema/context"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  7. http://www.springframework.org/schema/context
  8. http://www.springframework.org/schema/context/spring-context-2.5.xsd">
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注