Spring annotation configuration of java

A, Java annotations configuration is the Spring 4. the X- recommended configuration, you can completely replace the xml configuration. Spring of Java annotations configuration is through @Configuration and @Bean achieve these two notes: 
  . 1 , @Configuration at class, corresponding to a xml configuration file; 
  2 , @Bean acting on the method, which corresponds xml configuration < the bean > ; 
Example: The two annotations usage
 1, create entity classes: User.java
   public class User implements Serializable {
        private static final long serialVersionUID = 1L;
        private String username;
        private String password;
        private Integer age;
        // omitted set / get method
     }
 
 2, create Dao layer, simulation database access: UserDao.java
     public class UserDao {
        public List<User> findAll(){
           List<User> users = new ArrayList<User>();
           for(int i=1;i<10;i++) {
                User user = new User();
                user.setUsername(i+"号");
                user.setPassword(i+"1234");
                user.setAge(i+21);
                users.add(user);
           }
           return users;
        }
      } 
  3. Create Service Layer: UserService.java
    @Service
    public class UserService {
       @Autowired
       private UserDao userDao;
     
       public List<User> findAllUser(){
           
           return userDao.findAll();
         }
     }
 
  4, create class notes, the equivalent spring of the main configuration file applicationContext.xml
    @Configuration  // by the annotations to indicate the class is a Spring configuration, the equivalent of a xml file 
    @ComponentScan ( basePackages = "com.boot.service" )   // Configure scan package
    public class SpringAnnotation {
       @Bean  // 通过该注解来表明是一个Bean对象,相当于xml中的<bean>
       public UserDao getUserDao()
        {
           return new UserDao();
        }
     }
 
  5、测试:
   public class Main {
     public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringAnnotation.class);
           UserService userService = context.getBean(UserService.class);
           List<User> list = userService.findAllUser();
           for(User u : list) {
                System.out.println(u.getUsername()+"\t"+u.getPassword()+"\t"+u.getAge());
           }
           context.close();
      }
    }
 6、 从以上的示例中可以看出,使用Java代码就完美的替代xml配置文件,并且结构更加的清晰。
 
二、读取外部的资源配置文件, 通过@PropertySource可以指定读取的配置文件,通过@Value注解获取值,具体用法:
   @PropertySource(value= {"classpath:jdbc.properties"})
   public class SpringAnnotation {
      @Value("${jdbc.driver}")
      private String driver_class;
     
      @Value("${jdbc.url}")
      private String jdbc_url;
     
      @Value("${jdbc.username}")
      private String username;
     
      @Value("${jdbc.password}")
      private String password;
     
      @Bean
      public DataSource getDataSource() {
           DataSource dataSource = new DataSource();
           dataSource.setDriverClassName(driver_class);
           dataSource.setUrl(jdbc_url);
           dataSource.setUsername(username);
           dataSource.setPassword(password);
           return dataSource;
        }
     
      @Bean
      public SqlSessionFactoryBean getSqlSessionFactory() throws Exception {
           SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
           sessionFactory.setDataSource(this.getDataSource());  
           return sessionFactory;
       }

Guess you like

Origin www.cnblogs.com/lone5wolf/p/10937992.html