您现在的位置是:主页 > news > 企业自己怎么做网站推广/百度首页纯净版
企业自己怎么做网站推广/百度首页纯净版
admin2025/6/8 10:09:16【news】
简介企业自己怎么做网站推广,百度首页纯净版,做房地产自己要花钱开网站,wordpress 值班系统使用java的方式配置Spring: 使用这种方法就完全不需要Spring的xml配置了,完全让java来做 Configuration:意思是将config类交给Spring托管,注册到容器中,可以替代applicationContext.xml的一切功能 首先创建一个实体…
使用java的方式配置Spring:
使用这种方法就完全不需要Spring的xml配置了,完全让java来做
@Configuration:意思是将config类交给Spring托管,注册到容器中,可以替代applicationContext.xml的一切功能
首先创建一个实体类:
package pojo;import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;@Component
@Data
public class User {
@Value("张三")
private String name;
}
然后创建一个config类:所有的applicationContext.xml的功能都通过这个config类的标签代替
package config;import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import pojo.User;@Configuration
@ComponentScan("java")
public class pojo_config {
@Bean
public User user(){
return new User();
}
}
例如:
@ComponentScan(“java”)标签:就代表了扫描哪个包,在这个包下所有的标签都能生效,就相当于xml中的这行代码: <context:component-scan base-package=“java”/>
@Bean:注册一个bean,就相当于在xml里的一个bean标签,这个方法的名字就是bean的id,这个方法的返回值renturn的就是bean标签内的class属性,就是返回要注入的bean的对象
测试类:看到获取容器的类为AnnotationConfigApplicationContext,其中属性为config的class模板
@Test
public void getUser(){
ApplicationContext Context = new AnnotationConfigApplicationContext(pojo_config.class);
User user = Context.getBean("user", User.class);
System.out.println(user.toString());
}
如果完全使用配置类的方式去做,就只能通过AnnotationConfig上下文来获取容器,通过配置类的class对象加载