11,Spring - 基于注解的spring应用(bean的依赖注入注解开发)

在这里插入图片描述

1,@Value

1.1 新建配置文件 constant.properties

con.username=linailong

1.2 修改 applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd">


<!--    开启注解扫描 识别 @Component 通过springbean的后处理器实现-->
    <context:component-scan base-package="com.study"/>

    <context:property-placeholder location="classpath:constant.properties"/>
</beans>

1.3 修改 UserService

package com.study.service;

public interface UserService {
    void showUserName();
}

1.4 修改 UserServiceImpl

package com.study.service.impl;

import com.study.service.UserService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;


@Component("userService")
//@Scope("singleton") //是否是单例 prototype singleton
//@Lazy(value = true)
public class UserServiceImpl implements UserService {

    @Value("${con.username}")
    private String username;

    public UserServiceImpl() {
        System.out.println("UserServiceImpl 实例化");
    }

    public void showUserName() {
        System.out.println(username);
    }

    //    @PostConstruct
//    public void Construct(){
//        System.out.println("Construct--");
//    }
//
//    @PreDestroy
//    public void Destory(){
//        System.out.println("Destory -- ");
//    }
}

1.5 修改测试类 ApplicationContextDemo01

package com.study.demo;

import com.study.service.UserService;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ApplicationContextDemo01 {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context =
                new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService userService1 = (UserService)context.getBean("userService");
        userService1.showUserName();
    }
}

在这里插入图片描述

2,@Autowired

2.1 修改 UserService

package com.study.service;

public interface UserService {
    void showUserName();

    void showUserDao();
}

2.2 修改 UserServiceImpl

package com.study.service.impl;

import com.study.dao.UserDao;
import com.study.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;


@Component("userService")
public class UserServiceImpl implements UserService {

    @Value("${con.username}")
    private String username;

    @Autowired
//    @Qualifier("userDao")
//    @Resource()
    private UserDao userDao;

    public UserServiceImpl() {
        System.out.println("UserServiceImpl 实例化");
    }

    public void showUserName() {
        System.out.println(username);
    }

    public void showUserDao() {
        System.out.println(userDao);
    }

}


2.3 修改 ApplicationContextDemo01

package com.study.demo;

import com.study.service.UserService;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ApplicationContextDemo01 {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context =
                new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService userService1 = (UserService)context.getBean("userService");
        userService1.showUserDao();
    }
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值