02点睛Spring4.1-Java Config

1.1 java config

  • 使用上例建立的testMavenSpring项目,将pom.xml文件中的<spring-framework.version>3.2.3.RELEASE</spring-framework.version>修改为4.1.5.RELEASE,然后项目->右键->maven->update project;
  • spring的java config主要使用@Configuration@Bean两个注解;
    • 使用@Configuration注解在类上声明是一个配置类(相当于一个spring的配置xml);
    • 使用@Bean注解在方法上,返回值是一个类的实例,并声明这个返回值是spring的一个bean,bean的name是方法名;

1.2 关于@Bean和@Component,@Service,@Repository,@Controller

  • @Component,@Service,@Repository,@Controller注解在一个类上之后,这个类也成为spring容器中的bean,使用@Bean注解也是,感觉使用@Bean注解是不是更麻烦呢?

  • 既然效果是等同的,那什么时候使用@Bean什么时候使用@Component,@Service,@Repository,@Controller系列呢?

  • 这个原则就和我们当初混用xml配置和@Component,@Service,@Repository,@Controller时候一样:系统的全局配置(数据库配置,spring mvc配置,spring security配置等)使用java config(xml),业务相关的bean使用@Component,@Service,@Repository,@Controller系列。

  • 在后面我们讲到一些全局配置的时候我们就会使用Spring的java config

1.3 演示

1.3.1 创建一个properties文件作为配置

wisely.word = World

1.3.2 创建一个java class

public class DemoService {
    private String word;

    public String getWord() {
        return word;
    }

    public void setWord(String word) {
        this.word = word;
    }

    public String sayHello(){
        return "Hello "+this.word;
    }

}


1.3.3 创建java config配置类

package com.wisely.javaconfig;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;

@Configuration //声明是一个配置类
@PropertySource("com/wisely/javaconfig/test.properties")
public class DemoConfig {

    @Bean //声明是一个bean
    public DemoService demoBean(Environment environment){
        DemoService demoService = new DemoService();
        demoService.setWord(environment.getProperty("wisely.word"));
        return demoService;
    }
}


1.3.4 测试-初始化spring容器

package com.wisely.javaconfig;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Main {

    public static void main(String[] args) {
        //设定此包下的类被注册成spring的bean,包含@Configuration,@Component,@Service,@Repository,@Controller
        AnnotationConfigApplicationContext context =  new AnnotationConfigApplicationContext("com.wisely.javaconfig");
        DemoService demoService = context.getBean(DemoService.class);
        System.out.println(demoService.sayHello());
        context.close();
    }

}

输出结果:Hello World

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果你想在 `el-form-item` 中添加一个“显示/隐藏密码”按钮,让用户可以轻松地切换密码输入框的可见性,可以使用 `el-input` 组件的 `show-password` 属性和 `append` 插槽。 具体的实现步骤如下: 1. 在 `el-form-item` 中使用 `el-input` 组件,并将其 `type` 属性设置为密码类型,同时将其 `v-model` 绑定到你的数据模型中。 ```html <el-form-item label="密码"> <el-input type="password" v-model="password"></el-input> </el-form-item> ``` 2. 在 `el-input` 组件中添加 `show-password` 属性,以启用“显示/隐藏密码”按钮。 ```html <el-form-item label="密码"> <el-input type="password" v-model="password" show-password></el-input> </el-form-item> ``` 3. 在 `el-input` 组件中使用 `append` 插槽创建一个按钮,当点击该按钮时,可以切换密码输入框的可见性。 ```html <el-form-item label="密码"> <el-input type="password" v-model="password" show-password> <template v-slot:append> <i class="el-icon-view" v-show="!showPassword" @click="showPassword = true"></i> <i class="el-icon-view-off" v-show="showPassword" @click="showPassword = false"></i> </template> </el-input> </el-form-item> ``` 4. 在你的组件中定义一个 `showPassword` 变量,并在点击按钮时切换它的值。 ```javascript data() { return { password: '', showPassword: false } } ``` 这样,当用户点击“显示密码”按钮时,密码输入框将变为文本输入框,用户可以轻松地查看密码。而当用户再次点击该按钮时,密码输入框将再次变为密码输入框。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值