1. Comment
@ Contextconfiguration ("classpath: Beans. xml ")
Public class userdaotest extends actjunit4springcontexttests {
@ Resource (name = "userdao ")
Private userdao;
@ Test
Public void testsave (){
This. userdao. Save (new user ());
}
Note: If you use extends abstractjunit4springcontexttest, you do not need to read it again based on applicationcontext.
2. Support for generics when obtaining beans
@ Test
Public void testsave (){
Applicationcontext context = new classpathxmlapplicationcontext ("Beans. xml ");
Userdao = context. getbean ("userdao", userdao. Class );
// Userdao = (userdao) Context. getbean ("userdao"); (this was previously written)
Userdao. Save (new user ());
3. Regard a Java file as a configuration file
In beans. xml
<Context: component-scan base-package = "com. Liao. config"> </Context: component-scan>
Then, set the configuration file in the class:
Import org. springframework. Context. annotation. Bean;
Import org. springframework. Context. annotation. configuration;
Import com. bjsxt. spring30.dao. userdao;
Import com. bjsxt. spring30.dao. impl. userdaoimpl;
@ Configuration
Public class appconfig {
@ Bean
Public userdao (){
Return new userdaoimpl ();
}
// XML
// <Bean id = "userdao" class = "userdao ()"
}
Another example:
@ Bean
Public userdao (){
Return new userdaoimpl ();
}
@ Bean
Public userservice (){
Userserviceimpl service = new userserviceimpl ();
Service. setuserdao (userdao ());
Return service;
}
4 spel:
@ test
Public void test01 () {
expressionparser parser = new spelexpressionparser ();
Expression exp = parser. parseexpression ("'hello, world'");
system. out. println (string) exp. getvalue ();
}< br>
@ test
Public void test02 () {
User u = new user ();
U. setusername ("zhangsan");
evaluationcontext context = new standardevaluationcontext (U);
expressionparser parser = new spelexpressionparser ();
Expression exp = parser. parseexpression ("username"); // U. getUserName ()
system. out. println (string) exp. getvalue (context);
system. out. println (string) exp. getvalue (u);
5. In spel, you can assign and calculate some expressions in the configuration file, for example: