当前位置:   article > 正文

使用java方式配置spring_jdk.nashorn.internal.objects.annotations.construct

jdk.nashorn.internal.objects.annotations.constructor

1.创建实体类:User

  1. package com.huahua.pojo;
  2. import jdk.nashorn.internal.objects.annotations.Constructor;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.beans.factory.annotation.Value;
  5. import org.springframework.context.annotation.Configuration;
  6. import org.springframework.stereotype.Component;
  7. import java.beans.ConstructorProperties;
  8. //这个注解的意思,就是说这个类被spring接管,注册到了容器中
  9. @Component
  10. public class User {
  11. @Value("123")
  12. private String name;
  13. @Value("23")
  14. private int age;
  15. public User() {
  16. }
  17. @Override
  18. public String toString() {
  19. return "User{" +
  20. "name='" + name + '\'' +
  21. ", age=" + age +
  22. '}';
  23. }
  24. public User(String name, int age) {
  25. this.name = name;
  26. this.age = age;
  27. }
  28. public String getName() {
  29. return name;
  30. }
  31. public void setName(String name) {
  32. this.name = name;
  33. }
  34. public int getAge() {
  35. return age;
  36. }
  37. public void setAge(int age) {
  38. this.age = age;
  39. }
  40. }

2.创建配置类:

  1. package com.huahua.config;
  2. import com.huahua.pojo.User;
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.context.annotation.ComponentScan;
  5. import org.springframework.context.annotation.Configuration;
  6. import org.springframework.stereotype.Controller;
  7. //@Configuration 代表这是一个配置类
  8. @Configuration
  9. @ComponentScan("com.huahua")//扫描包下面的注释
  10. public class Config {
  11. //注册一个bean 相当于一个bean标签
  12. //这个方法的名字 这个bean的id
  13. //这个方法的返回值 就是bean的class
  14. @Bean
  15. public User getUser(){
  16. return new User();
  17. }
  18. }

2.创建测试类:

  1. package com.huahua;
  2. import com.huahua.config.Config;
  3. import com.huahua.pojo.User;
  4. import org.junit.Test;
  5. import org.springframework.context.ApplicationContext;
  6. import org.springframework.context.annotation.AnnotationConfigApplicationContext;
  7. public class MyTest {
  8. @Test
  9. public void test(){
  10. ApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
  11. User getUser = context.getBean("getUser", User.class);
  12. System.out.println(getUser.toString());
  13. }
  14. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/226940
推荐阅读
相关标签
  

闽ICP备14008679号