File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ 写代码实现Spring Bean的装配,方式越多越好(XML、Annotation都可以)
Original file line number Diff line number Diff line change 1+ package homework0902 ;
2+
3+ import homework0902 .pojo .Student ;
4+ import homework0902 .pojo .Student1 ;
5+ import homework0902 .pojo .Student2 ;
6+ import homework0902 .pojo .StuConfig ;
7+ import org .springframework .context .ApplicationContext ;
8+ import org .springframework .context .annotation .AnnotationConfigApplicationContext ;
9+ import org .springframework .context .support .ClassPathXmlApplicationContext ;
10+
11+ public class Homework0902 {
12+ public static void main (String [] args ) {
13+ ApplicationContext applicationContext = new ClassPathXmlApplicationContext ("hwApplicationContext.xml" );
14+ ApplicationContext configContext = new AnnotationConfigApplicationContext (StuConfig .class );
15+
16+ Student student1 = (Student1 ) applicationContext .getBean ("student1" );
17+ System .out .println ("方法1:" + student1 .toString ());
18+
19+ Student student2 = (Student2 ) applicationContext .getBean ("student2" );
20+ System .out .println ("方法2:" + student2 .toString ());
21+
22+ Student student3 = applicationContext .getBean (Student2 .class );
23+ System .out .println ("方法3:" + student3 .toString ());
24+
25+ Student student4 = (Student ) configContext .getBean ("stu1" );
26+ System .out .println ("方法4:" + student4 .toString ());
27+
28+ }
29+
30+
31+ }
Original file line number Diff line number Diff line change 1+ package homework0902 .pojo ;
2+
3+ import org .springframework .context .annotation .Bean ;
4+ import org .springframework .context .annotation .Configuration ;
5+
6+ @ Configuration
7+ public class StuConfig {
8+ @ Bean (name = "stu1" )
9+ public Student getStudent () {
10+ return new Student1 (3 ,"xiyuan3" );
11+ }
12+
13+ }
Original file line number Diff line number Diff line change 1+ package homework0902 .pojo ;
2+
3+
4+ import java .io .Serializable ;
5+
6+ public interface Student extends Serializable {
7+ void dingdo ();
8+ }
Original file line number Diff line number Diff line change 1+ package homework0902 .pojo ;
2+
3+ import lombok .AllArgsConstructor ;
4+ import lombok .Data ;
5+ import lombok .NoArgsConstructor ;
6+ import lombok .ToString ;
7+
8+ @ Data
9+ @ AllArgsConstructor
10+ @ NoArgsConstructor
11+ @ ToString
12+ public class Student1 implements Student {
13+
14+ private int id ;
15+ private String name ;
16+
17+ @ Override
18+ public void dingdo () {
19+ System .out .println ("Student1" );
20+ }
21+ }
Original file line number Diff line number Diff line change 1+ package homework0902 .pojo ;
2+
3+ import lombok .AllArgsConstructor ;
4+ import lombok .Data ;
5+ import lombok .NoArgsConstructor ;
6+ import lombok .ToString ;
7+ import org .springframework .beans .factory .annotation .Value ;
8+ import org .springframework .stereotype .Component ;
9+
10+ @ Data
11+ @ AllArgsConstructor
12+ @ NoArgsConstructor
13+ @ ToString
14+ @ Component (value = "student2" )
15+ public class Student2 implements Student {
16+ @ Value ("002" )
17+ private int id ;
18+ @ Value ("西元2" )
19+ private String name ;
20+
21+ @ Override
22+ public void dingdo () {
23+ System .out .println ("Student2" );
24+ }
25+ }
Original file line number Diff line number Diff line change 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+ xmlns : context =" http://www.springframework.org/schema/context"
5+ xmlns : aop =" http://www.springframework.org/schema/aop"
6+ xsi : schemaLocation =" http://www.springframework.org/schema/beans
7+ http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
8+ http://www.springframework.org/schema/context
9+ http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd" >
10+
11+ <bean id =" student1"
12+ class =" homework0902.pojo.Student1" >
13+ <property name =" id" value =" 001" />
14+ <property name =" name" value =" 西元1" />
15+ </bean >
16+
17+ <context : component-scan base-package =" homework0902.pojo" />
18+
19+ </beans >
You can’t perform that action at this time.
0 commit comments