后端开发基础-Spring框架学习-004——基础概念

@Component(“wt_low”)

public class Waiter_low extends Waiter{

public Waiter_low() {

System.out.println(“Waiter_low的无参构造器…”);

}

}

Restaurant.java

package annotation;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.beans.factory.annotation.Qualifier;

import org.springframework.stereotype.Component;

/**

  • 演示使用@Autowired注解来

  • 完成构造器方式的注入。

  • @author Cher_du

*/

@Component(“rest”)

public class Restaurant {

private Waiter wt2;

public Restaurant() {

System.out.println(“Restaurant的无参构造器…”);

}

@Autowired(required=false)

//@Qualifier指定要注入的bean的id

public Restaurant(@Qualifier(“wt_low”) Waiter wt2) {

System.out.println(“Restaurant的有参构造器…”);

this.wt2 = wt2;

}

@Override

public String toString() {

return “Restaurant [wt=” + wt2 + “]”;

}

}

Restaurant2.java

package annotation;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.beans.factory.annotation.Qualifier;

import org.springframework.stereotype.Component;

/**

  • 演示使用@Autowired注解来完成

  • set方式的注入。

  • @author Cher_du

*/

@Component(“rest2”)

public class Restaurant2 {

@Autowired

@Qualifier(“wt”)

private Waiter wt;

public Restaurant2() {

System.out.println(“Restaurant2的无参构造器…”);

}

public Waiter getWt() {

return wt;

}

@Autowired

public void setWt(@Qualifier(“wt”) Waiter wt) {

System.out.println(“Restaurant2的setWt方法…”);

this.wt = wt;

System.out.println(“setWt—over…”);

}

@Override

public String toString() {

return “Restaurant2 [wt=” + wt + “]”;

}

}

Computer.java

package annotation;

import org.springframework.stereotype.Component;

@Component(“cp”)

public class Computer {

public Computer() {

System.out.println(“Computer的无参构造器…”);

}

}

Manager.java

package annotation;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Value;

import org.springframework.stereotype.Component;

/**

  • 演示使用@Resource注解来完成set方式的注入。

  • 只支持set方式注入。

  • @author Cher_du

*/

@Component(“mg”)

public class Manager {

private Computer cp;

@Value(“#{jdbc.pageSize}”)

//使用Value注解来赋值。

//Value注解可以使用spring表达式

private int pageSize;

@Value(“宝玉”)

//Value注解还可以完成基本类型的值的注入

private String name;

public Manager() {

System.out.println(“Manager的无参构造器…”);

}

public Computer getCp() {

return cp;

}

@Resource(name=“cp”)

//name属性指定要注入的bean的id

public void setCp(Computer cp) {

System.out.println(“Manager的setCp方法…”);

this.cp = cp;

}

public int getPageSize() {

return pageSize;

}

public void setPageSize(int pageSize) {

this.pageSize = pageSize;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

@Override

public String toString() {

return “Manager [cp=” + cp + “, pageSize=” + pageSize + “, name=” + name + “]”;

}

}

app.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”

xmlns:jdbc=“http://www.springframework.org/schema/jdbc”

xmlns:jee=“http://www.springframework.org/schema/jee”

xmlns:tx=“http://www.springframework.org/schema/tx”

xmlns:aop=“http://www.springframework.org/schema/aop”

xmlns:mvc=“http://www.springframework.org/schema/mvc”

xmlns:util=“http://www.springframework.org/schema/util”

xmlns:jpa=“http://www.springframework.org/schema/data/jpa”

xsi:schemaLocation="

http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd

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

http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd

http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd

http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd

http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd

http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd

http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd

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

<context:component-scan

base-package=“annotation”/>

<util:properties id=“jdbc”

一线互联网大厂Java核心面试题库

image

正逢面试跳槽季,给大家整理了大厂问到的一些面试真题,由于文章长度限制,只给大家展示了部分题目,更多Java基础、异常、集合、并发编程、JVM、Spring全家桶、MyBatis、Redis、数据库、中间件MQ、Dubbo、Linux、Tomcat、ZooKeeper、Netty等等已整理上传,感兴趣的朋友可以看看支持一波!

包下面的所有的类,如果该类前面有特定的

注解(比如@Component),则容器会将其纳入

进行管理(相当在配置文件当中,有一个bean)。

–>

<context:component-scan

base-package=“annotation”/>

<util:properties id=“jdbc”

一线互联网大厂Java核心面试题库

[外链图片转存中…(img-RLJDVzQD-1714190623705)]

正逢面试跳槽季,给大家整理了大厂问到的一些面试真题,由于文章长度限制,只给大家展示了部分题目,更多Java基础、异常、集合、并发编程、JVM、Spring全家桶、MyBatis、Redis、数据库、中间件MQ、Dubbo、Linux、Tomcat、ZooKeeper、Netty等等已整理上传,感兴趣的朋友可以看看支持一波!

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

  • 25
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值