The application of spring in Java Engineering

Source: Internet
Author: User

1, a new Java project: Springjavatest.

2, add spring2.5 Package (2.5 version can be injected with the annotation)

3, new two JavaBean:Room.java and House.java, as follows:

Package Com.bean; public class Room {private String area = "100 square meters";//Action public string say () {return "Me area is" +area;} Public String Getarea () {return area;} public void Setarea (String area) {This.area = area;}}

Package Com.bean; Import org.springframework.beans.factory.annotation.Autowired; public class House {@Autowired private Room Room, @Override public String toString () {return Room.say ()}}

4, modify the configuration file Applicationcontext.xml

<?xml version= "1.0" encoding= "UTF-8"?> <beans "xmlns=" 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-2.5.xsd "> <!--the beanpostprocessor will automatically function, to the callout @ autowired beans are automatically injected--> <bean class= " Org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor "/> <bean id=" Room "class=" "Com.bean.Room" > <property name= "area" value= "860 sqm/> </bean> <bean id=" house "class=" Com.bean.House "> </bean> </beans>

5, New test class Springtest.java

Package com.test import org.springframework.context.ApplicationContext; import Org.springframework.context.support.ClassPathXmlApplicationContext; Import Com.bean.House; public class Springtest {public static void main (string[] args) {string[] locations = {"Applicationcontext.xml"}; ApplicationContext context = new Classpathxmlapplicationcontext (locations); House House = [House] Context.getbean ("house"); System.out.println (house); } }

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.