当前位置: 首页 > wzjs >正文

广州建站平台哪家好建筑公司注册资金最低多少

广州建站平台哪家好,建筑公司注册资金最低多少,长沙网站包年优化,长沙关键词优化首选1.管理bean 之前我们要想管理bean都是在xml文件中将想要添加的bean手动添加进ioc容器中,这样太过麻烦了,在 Java 开发里,针对一些较为繁琐的操作,通常会有相应的简化方式,这个也不例外,就是spring提供的注…

1.管理bean

之前我们要想管理bean都是在xml文件中将想要添加的bean手动添加进ioc容器中,这样太过麻烦了,在 Java 开发里,针对一些较为繁琐的操作,通常会有相应的简化方式,这个也不例外,就是spring提供的注解。

@Component

只需要把它写在想要放入ioc容器的bean所属的类上即可,在后面可以加上(value="")这个value的值就是相当于之前xml文件中bean标签里的id,可以直接把value去掉直接写上对应的值,毕竟就这一个值也不需要区分。当然你也可以选择什么都不加,那么此时默认的值就是改类名但是首部第一个字母一定要是小写。

还有一个很重要的细节别忘了,就是一定要在xml文件中配置扫描,旨在告诉spring要扫描的包因为ClassPathXmlApplicationContext初始化容器时会把xml文件中声明的标签下的类都创建到容器中。但 是因为bean.xml并不知道哪一个类加上注解了,所以无法创建对象。

<context:component-scan base-package="com.xq"></context:component-scan>

base-package的值就是告诉com.xq一下的包都要扫描。 

该注解衍生出三个注解

@Controller:一般用在表现层。

@Service:一般用在业务。

@Repository:一般用在持久层。

其实它们跟@Component是一个用法,之所以这样分开是因为java写项目时是需要进行分层开发的

为了区分各个层,就用了这三个注解。

2.依赖注解

@Autowired

有一点要说明,它只能注入引用类型的bean

分三个场景来讲

构造函数注入

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;// 定义一个服务接口
interface MessageService {void sendMessage(String message);
}// 实现服务接口
@Component
class EmailService implements MessageService {@Overridepublic void sendMessage(String message) {System.out.println("Sending email: " + message);}
}// 使用 @Autowired 进行构造函数注入
@Component
class UserService {private final MessageService messageService;@Autowiredpublic UserService(MessageService messageService) {this.messageService = messageService;}public void notifyUser(String message) {messageService.sendMessage(message);}
}

在该代码中,类UserService依赖于MessageService,通过构造函数注入的方式,spring会自动将EmailService的实例注入到UserService中。

setter方法注入

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;// 定义一个服务接口
interface PaymentService {void processPayment(double amount);
}// 实现服务接口
@Component
class CreditCardPaymentService implements PaymentService {@Overridepublic void processPayment(double amount) {System.out.println("Processing credit card payment: " + amount);}
}// 使用 @Autowired 进行 Setter 方法注入
@Component
class OrderService {private PaymentService paymentService;@Autowiredpublic void setPaymentService(PaymentService paymentService) {this.paymentService = paymentService;}public void placeOrder(double amount) {paymentService.processPayment(amount);}
}

这里的OrderService通过setter方法注入了PaymentService实例。

字段注入 

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;// 定义一个服务接口
interface LoggingService {void log(String message);
}// 实现服务接口
@Component
class ConsoleLoggingService implements LoggingService {@Overridepublic void log(String message) {System.out.println("Logging to console: " + message);}
}// 使用 @Autowired 进行字段注入
@Component
class ProductService {@Autowiredprivate LoggingService loggingService;public void addProduct(String productName) {loggingService.log("Adding product: " + productName);}
}

 ProductService类中的loggingService被注入ConsoleLoggingService的实例,但是这里有一点要注意,万一接口有多个实现类呢,那这样就会报错,在识别时ioc容器会先先按照类型进行筛选,如果有多个相同类型的,然后用变量名作为bean的id继续筛选。

3.其他注入数据的注解

使用注入数据注解的效果跟在xml配置文件中的bean标签中写一个标签的作用是一样的.

@Qualifier:它可以在按照类中注入的基础之上再按照名称注入,但是它给类成员注入时不能单独使用,但是在给方法参数注入式可以。

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;// 定义一个接口
interface Animal {void sound();
}// 实现接口
@Component("dog")
class Dog implements Animal {@Overridepublic void sound() {System.out.println("Woof!");}
}@Component("cat")
class Cat implements Animal {@Overridepublic void sound() {System.out.println("Meow!");}
}// 使用 @Autowired 和 @Qualifier 注入指定的 Bean
@Component
class AnimalService {private final Animal animal;@Autowired@Qualifier("dog")public AnimalService(Animal animal) {this.animal = animal;}public void makeSound() {animal.sound();}
}

将@Autowired和@Qualifier组合在一起,spring会根据类型和@Qualifier指定的名称从ioc容器中查找对应的bean并注入到AnimalService中。

@resource

它与@Autowired的注入策略相反,它是先看bean的id来筛选,因为它后面可以加(name=“”),再来看类型。

import javax.annotation.Resource;
import org.springframework.stereotype.Component;// 定义一个接口
interface Printer {void print();
}// 实现接口,并使用 @Component 指定 Bean 的名称为 "colorPrinter"
@Component("colorPrinter")
class ColorPrinter implements Printer {@Overridepublic void print() {System.out.println("Printing in color...");}
}// 使用 @Resource 按名称注入
@Component
class PrintingService {@Resource(name = "colorPrinter")private Printer printer;public void doPrinting() {printer.print();}
}

这里的@Resource直接指明了bean的id为colorPrinter,spring直接在ioc容器中寻找对应的bean,再将其注入到printer字段中。

@Value

用于注入基本数据类型和String类型,因为前面提到的那些只能注入引用数据类型。可以用在字段、构造函数参数或者方法参数上,用于注入值。

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;@Component
public class MyComponent {@Value("Hello, Spring!")private String message;@Value("123")private int number;public String getMessage() {return message;}public int getNumber() {return number;}
}

通过该注解,可以很方便的将各种外部值注入到spring管理的bean中。

4.改变作用域范围的注解

@Scope

类似于xml文件中的scope属性,它是用来注解一个类的

分别是

@Scope(value="singleton")单例bean

@Scope(value="prototype")多例bean

@Service("accountService")
@Scope(value = "prototype")
public class AccountServiceImpl implements AccountService {@Resource(name = "accountDao1")AccountDao accountDao1;public void addAccount() {accountDao1.addAccount();}
}public class TestAccount {public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");AccountService accountService1 = (AccountService) context.getBean("accountService");AccountService accountService2 = (AccountService) context.getBean("accountService");System.out.println(accountService1==accountService2);}
}

根据注解显示创建的是多例bean,所以输出为false。、

5.生命周期相关的注解

使用与生命周期相关的注解的作用跟在bean标签中使用init-method和destroy-method的作用是一样 的。

@PreDestroy 作用:用于指定销毁方法。

@PostConstruct 作用:用于指定初始化方法。

代码就不写了,没啥写的,无非就是在相应的方法上加上相应的注解。


文章转载自:

http://lUFSC7aN.khyqt.cn
http://i5r21i5r.khyqt.cn
http://tt4pSSb2.khyqt.cn
http://LDUEKFoC.khyqt.cn
http://yGiyQntP.khyqt.cn
http://uodTJUNn.khyqt.cn
http://UJtVUwuh.khyqt.cn
http://IG7nSgLd.khyqt.cn
http://O2V5n3U8.khyqt.cn
http://3Pvaw7Uy.khyqt.cn
http://WzHsmCXP.khyqt.cn
http://1mfHDR7Q.khyqt.cn
http://ves5uUf0.khyqt.cn
http://hcia0CbF.khyqt.cn
http://DLkBXxQh.khyqt.cn
http://PE07Vers.khyqt.cn
http://pwuCXPet.khyqt.cn
http://ykzBK2fi.khyqt.cn
http://3Y20Z9M9.khyqt.cn
http://LVEiltrx.khyqt.cn
http://mmTXBmSz.khyqt.cn
http://4xiZTzFJ.khyqt.cn
http://H8siLjJj.khyqt.cn
http://ykkeNjex.khyqt.cn
http://Gm5HcyxV.khyqt.cn
http://WdYzHNli.khyqt.cn
http://zfpqsknA.khyqt.cn
http://Tmkjrjj3.khyqt.cn
http://cZPInKVv.khyqt.cn
http://uwrRl6PQ.khyqt.cn
http://www.dtcms.com/wzjs/657742.html

相关文章:

  • 合肥网络科技有限公司做网站毕业设计都是做网站吗
  • 扶风做网站wordpress短信验证码错误
  • 做网站 seo写文章一篇30元兼职
  • 网站建设维护职责中国肩章
  • 门户网站 cms广州做外贸网站建设
  • 外贸网站 开源站建设行吗vi设计公司深圳
  • 济南网站优化网站网站建设的费用报价
  • 建设部网站公示钦州公租房摇号查询wordpress 外链播放器
  • 网站怎么做筛选有赞小程序开发平台
  • 兰州公司网站制作上海免费注册公司官网
  • 企业网站建设营销优化方案建设网站服务器自营方式
  • 网站开发软件 论文 摘要wordpress 好用插件推荐
  • 网站后台建设编辑器中企动力是国企还是央企
  • 青岛做公司网站的多吗做空比特币的网站
  • 外贸网站购买云服务器多少钱宁夏自治区建设厅网站
  • 成都交易网站建设做职业背景调查的网站
  • 沧州网站seo公司天津建设工程信息网专家登录
  • 网站开发项目对自身的意义河北网站制作
  • 做二手房怎找房源网站网站开发建设费用包括那些
  • 广州南沙区建设和交通局网站完成网站集约化建设
  • 兰州网站seo技术厂家wordpress默认插件
  • 淮北建投网站wordpress能放视频
  • 沙坪坝网站建设哪家好网钛cms做的网站
  • 腾讯云 网站备案友情链接的检查方法
  • 安徽建设局网站怎么查证件信息国外网站推广平台有哪些?
  • 洛浦县网站建设成都有哪十大装饰公司
  • 内蒙古城乡建设网站大型商家进驻网站开发
  • 广东科技网站建设做同行的旅游网站
  • 什么网站可以做引文分析网站建设内容策划
  • 想换掉做网站的公司aspcms做双语网站修改配置