1. The need to add a profile in the Spring (the applicationContext.xml) in which:
<! - automatically scan packages at specified annotation -> < context: Scan-Component Base-Package = "com.xbf.demo" />
2. Next, create the entity classes directly in the corresponding packet, and adds the comment:
User . java
// equivalent to: add an object Spring container // <the bean ID = "User" class = "com.xbf.demo.User" /> the Component: Component Bean Contorller: Web Layer Service: service layer Repository: dao layer @Component ( "User" ) public class the User { public String name = "XBF" ; }
3.IOC injection:
3.1 may not be set to provide a method, @Value ( "attribute value") directly on the attribute name
@Component("user2") public class User2{ @Value("xbf2") private String name; } //相当于: <bean id="user2" class="com.xbf.Demo.User2"> <property name="name" value="xbf2"/> </bean>
3.2 if set method, the above method is directly written in the set: @Value ( "attribute value")
@Controller("user2") public class User2 { // <bean class="com.kuang.demo.User2" id="user2"> // <property name="name" value="秦疆2号"/> // </bean> private String name; public String getName() { return name; } @Value("秦疆") public void setName(String name) { this.name = name; } }