文章目录  UserController.java UserMapperImpl.java School.java Student.java User1ServiceImpl.java UserServiceImpl.java AnnotationContext.xml ApplicationContext.xml UserControllerTest.java StudentTest.java pom.xml   
 
package  com. aistart. controller ; import  com. aistart. pojo.  Student ; 
import  com. aistart. service.  UserService ; 
import  com. aistart. service. impl.  UserServiceImpl ; 
import  org. springframework. beans. factory. annotation.  Autowired ; 
import  org. springframework. beans. factory. annotation.  Qualifier ; 
import  org. springframework. stereotype.  Controller ; @Controller 
public  class  UserController  { @Autowired @Qualifier ( "userServiceImpl" ) UserService  userService; public  void  setUserService ( UserService  userService)  { this . userService =  userService; } public  String  Login ( Student  student) { boolean  login =  userService. login ( student) ; if  ( login) return  "yes" ; else  return  "no" ; } } package  com. aistart. mapper. impl ; import  com. aistart. mapper.  UserMapper ; 
import  com. aistart. pojo.  Student ; 
import  org. springframework. stereotype.  Repository ; @Repository 
public  class  UserMapperImpl  implements  UserMapper  { @Override public  int  selectUser ( Student  student)  { System . out. println ( "这里是查找是否有用户的mapper" ) ; return  0 ; } 
} 
package  com. aistart. pojo ; public  class  School  { private  String  schoolName; public  School ( String  schoolName)  { this . schoolName =  schoolName; } public  School ( )  { System . out. println ( "school的无参被调用了" ) ; } public  String  getSchoolName ( )  { return  schoolName; } public  void  setSchoolName ( String  schoolName)  { this . schoolName =  schoolName; } @Override public  String  toString ( )  { final  StringBuilder  sb =  new  StringBuilder ( "School{" ) ; sb. append ( "schoolName='" ) . append ( schoolName) . append ( '\'' ) ; sb. append ( '}' ) ; return  sb. toString ( ) ; } 
} 
package  com. aistart. pojo ; import  org. springframework. stereotype.  Component ; 
@Component 
public  class  Student  { private  String  stuName; private  Integer  age; private  School  school; public  School  getSchool ( )  { return  school; } public  void  setSchool ( School  school)  { this . school =  school; } public  Student ( )  { System . out. println ( "student无参" ) ; } public  Student ( String  stuName,  Integer  age)  { System . out. println ( "全参数构造" ) ; this . stuName =  stuName; this . age =  age; } public  String  getStuName ( )  { return  stuName; } public  void  setStuName ( String  stuName)  { this . stuName =  stuName; } public  Integer  getAge ( )  { return  age; } public  void  setAge ( Integer  age)  { this . age =  age; } @Override public  String  toString ( )  { final  StringBuilder  sb =  new  StringBuilder ( "Student{" ) ; sb. append ( "stuName='" ) . append ( stuName) . append ( '\'' ) ; sb. append ( ", age=" ) . append ( age) ; sb. append ( ", school=" ) . append ( school) ; sb. append ( '}' ) ; return  sb. toString ( ) ; } 
} 
package  com. aistart. service. impl ; import  com. aistart. mapper.  UserMapper ; 
import  com. aistart. pojo.  Student ; 
import  com. aistart. service.  UserService ; 
import  org. springframework. stereotype.  Service ; import  javax. annotation.  Resource ; @Service 
public  class  User1ServiceImpl  implements  UserService  { @Resource UserMapper  userMapper; public  void  setUserMapper ( UserMapper  userMapper)  { this . userMapper =  userMapper; } @Override public  boolean  login ( Student  student)  { System . out. println ( "这是login的service业务" ) ; int  i =  userMapper. selectUser ( student) ; if  ( i== 1 ) { return  true ; } return  false ; } 
} package  com. aistart. service. impl ; import  com. aistart. mapper.  UserMapper ; 
import  com. aistart. mapper. impl.  UserMapperImpl ; 
import  com. aistart. pojo.  Student ; 
import  com. aistart. service.  UserService ; 
import  org. springframework. stereotype.  Service ; import  javax. annotation.  Resource ; @Service 
public  class  UserServiceImpl  implements  UserService  { @Resource UserMapper  userMapper; public  void  setUserMapper ( UserMapper  userMapper)  { this . userMapper =  userMapper; } @Override public  boolean  login ( Student  student)  { System . out. println ( "这是login的service业务" ) ; int  i =  userMapper. selectUser ( student) ; if  ( i== 1 ) { return  true ; } return  false ; } 
} 
<?xml version="1.0" encoding="UTF-8"?> 
< beansxmlns = " http://www.springframework.org/schema/beans" xmlns: xsi= " http://www.w3.org/2001/XMLSchema-instance" xmlns: context= " http://www.springframework.org/schema/context" xsi: schemaLocation= " http://www.springframework.org/schema/beanshttps://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttps://www.springframework.org/schema/context/spring-context.xsd" > < context: component-scanbase-package = " com.aistart" > </ context: component-scan> < context: annotation-config/> </ beans> <?xml version="1.0" encoding="UTF-8"?> 
< beansxmlns = " 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" > < beanid = " alice" class = " com.aistart.pojo.Student" > < constructor-argindex = " 0" value = " Candy" /> < constructor-argindex = " 1" value = " 19" /> < propertyname = " stuName" value = " Alice" /> < propertyname = " age" value = " 19" /> < propertyname = " school" ref = " ksdx" /> </ bean> < beanid = " sqxy" class = " com.aistart.pojo.School" > < propertyname = " schoolName" value = " 小清华" > </ property> </ bean> < beanid = " ksdx" class = " com.aistart.pojo.School" > < propertyname = " schoolName" value = " 呼呼睡觉" > </ property> </ bean> < beanid = " userController" class = " com.aistart.controller.UserController" > < propertyname = " userService" ref = " userService" > </ property> </ bean> < beanid = " userService" class = " com.aistart.service.impl.UserServiceImpl" > < propertyname = " userMapper" ref = " userMapper" > </ property> </ bean> < beanid = " userMapper" class = " com.aistart.mapper.impl.UserMapperImpl" /> < beanid = " newOne" class = " com.aistart.pojo.Student" scope = " prototype" > < constructor-argname = " age" value = " 19" > </ constructor-arg> < constructor-argname = " stuName" value = " candy" > </ constructor-arg> </ bean> </ beans> 
package  com. aistart. controller ; import  com. aistart. pojo.  Student ; 
import  org. junit.  Test ; 
import  org. springframework. context.  ApplicationContext ; 
import  org. springframework. context. support.  ClassPathXmlApplicationContext ; import  static  org. junit.  Assert . * ; public  class  UserControllerTest  { @Test public  void  login ( )  { ApplicationContext  context =  new  ClassPathXmlApplicationContext ( "AnnotationContext.xml" ) ; UserController  userController =  ( UserController )  context. getBean ( "userController" ) ; String  login =  userController.  Login( new  Student ( "小米" , 1 ) ) ; System . out. println ( login) ; } 
} package  com. aistart. pojo ; import  org. junit.  Test ; 
import  org. springframework. beans.  BeanUtils ; 
import  org. springframework. context.  ApplicationContext ; 
import  org. springframework. context. annotation.  Bean ; 
import  org. springframework. context. support.  ClassPathXmlApplicationContext ; import  static  org. junit.  Assert . * ; public  class  StudentTest  { @Test public  void  test ( ) { Student  alice =  new  Student ( "Alice" ,  18 ) ; Student  student =  new  Student ( "小谬" , 1 ) ; student. setStuName ( "Bob" ) ; student. setAge ( 19 ) ; } @Test public  void  beanTest ( ) { ApplicationContext  context =  new  ClassPathXmlApplicationContext ( "ApplicationContext.xml" ) ; Student  new1 = ( Student )  context. getBean ( "newOne" ) ; Student  new2 = ( Student )  context. getBean ( "newOne" ) ; Student  alice1 = ( Student )  context. getBean ( "alice" ) ; System . out. println ( new1== new2) ; } @Test public  void  annoTest ( ) { ApplicationContext  context =  new  ClassPathXmlApplicationContext ( "AnnotationContext.xml" ) ; } 
} 
<?xml version="1.0" encoding="UTF-8"?> 
< projectxmlns = " http://maven.apache.org/POM/4.0.0" xmlns: xsi= " http://www.w3.org/2001/XMLSchema-instance" xsi: schemaLocation= " http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > < modelVersion> </ modelVersion> < groupId> </ groupId> < artifactId> </ artifactId> < version> </ version> < properties> < maven.compiler.source> </ maven.compiler.source> < maven.compiler.target> </ maven.compiler.target> < project.build.sourceEncoding> </ project.build.sourceEncoding> </ properties> < dependencies> < dependency> < groupId> </ groupId> < artifactId> </ artifactId> < version> </ version> < scope> </ scope> </ dependency> < dependency> < groupId> </ groupId> < artifactId> </ artifactId> < version> </ version> </ dependency> </ dependencies> </ project>