需求:

bean对象本身,移出beans.xml配置文件,也通过注解进行。

创建文件:

同注入对象章节。
1.创建com.tutorialspoint包
2.创建HelloWorld类
3.创建MainAPP主类(void main)
4.创建Beans.xml
5.ModelTest类

代码:

Beans

删掉之前留下的代码,新增<context:component-scan base-package="com.tutorialspoint"/>
其作用是告诉Spring,bean都放在com.tutorialspoint这个包下

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
  4. xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
  5. xsi:schemaLocation="
  6. http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  8. http://www.springframework.org/schema/aop
  9. http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
  10. http://www.springframework.org/schema/tx
  11. http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
  12. http://www.springframework.org/schema/context
  13. http://www.springframework.org/schema/context/spring-context-3.0.xsd">
  14. <context:component-scan base-package="com.tutorialspoint"/>
  15. </beans>

HelloWorld.java

为HelloWorld类加上@Component注解,即表明此类是bean,name为helloworld。

  1. package com.tutorialspoint;
  2. import org.springframework.stereotype.Component;
  3. @Component("helloworld")
  4. public class HelloWorld {
  5. private String message = "helloworld";
  6. public void setMessage(String message){
  7. this.message = message;
  8. }
  9. public void getMessage(){
  10. System.out.println("Your Message : " + message);
  11. }
  12. }

ModelTest.java

为ModelTest类加上@Component注解,即表明此类是bean,name为modeltest。
@Autowired保留。

  1. package com.tutorialspoint;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.stereotype.Component;
  4. @Component("modeltest")
  5. public class ModelTest {
  6. private String str = "modeltest";
  7. @Autowired
  8. private HelloWorld helloWorld;
  9. public void setStr(String string){
  10. this.str = string;
  11. }
  12. public void getStr(){
  13. System.out.println("Your str : " + str);
  14. }
  15. public void setHelloWorld(HelloWorld helloworld){
  16. this.helloWorld = helloworld;
  17. }
  18. public void getHelloWorld(){
  19. helloWorld.getMessage();
  20. }
  21. }

MainApp.java

保留不变,和原来一样。

  1. package com.tutorialspoint;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. public class MainApp {
  5. public static void main(String[] args) {
  6. // TODO Auto-generated method stub
  7. ApplicationContext context =
  8. new ClassPathXmlApplicationContext("Beans.xml");
  9. HelloWorld obj = (HelloWorld) context.getBean("helloworld");
  10. obj.getMessage();
  11. ModelTest model = (ModelTest) context.getBean("modeltest");
  12. model.getStr();
  13. model.getHelloWorld();
  14. }
  15. }

运行结果:

图片.png

要点:

@component一般用来配置