Spring 3.0 new feature learning 1

Source: Internet
Author: User

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:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.