日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

java 日期注解 xml_Spring xml注解+java注解

發(fā)布時(shí)間:2025/3/12 javascript 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java 日期注解 xml_Spring xml注解+java注解 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1,xml+基于注解配置(第一步:在xml開啟注解配置)

@Component?  被表示類會(huì)被納入 spring ioc 容器進(jìn)行管理,相當(dāng)于

@Value    ? ?為類注入基本類型和String屬性值

@Autowired   為類注入應(yīng)用類型屬性值;默認(rèn)按類型注入,可以通過@Qualifier("dog2")指定bean,同時(shí)也可以通過bean加入primary=true 優(yōu)先被Autowired注入

@Resource  ? ? 默認(rèn)按類型注入,如果指定了name屬性,則按bean名稱注入

例:

添加配置文件beans-annotation.xml

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

https://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/context

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

添加Dog類

@Component

public class Dog {

@Value("旺財(cái)")

private String name;

@Value("公")

private String sex;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getSex() {

return sex;

}

public void setSex(String sex) {

this.sex = sex;

}

@Override

public String toString() {

return "entity.Dog [name=" + name + ", sex=" + sex + "]";

}

}

添加測(cè)試類

public static void main(String[] args) {

ApplicationContext context = new ClassPathXmlApplicationContext("beans-annotation.xml");

Dog dog = (Dog) context.getBean("dog");

System.out.println(dog.toString());

}

2,java+注解配置(主流,第一步:在配置類中開啟注解掃描)

@ComponentScan("entity")  會(huì)去entity掃描@Component @Value @Autowired進(jìn)行創(chuàng)建bean或注入屬性值

注意:需要再多導(dǎo)入一個(gè)包,spring-app-5.0.14.RELEASE.jar

1)AnnotationConfigApplicationContext

@Configuration  指定該類為Spring ioc容器配置類,相當(dāng)于beans.xml文件

@Bean(name ="user")  將方法返回值納入到spring ioc容器進(jìn)行管理,相當(dāng)于

例如:

添加配置文件beans-annotation.xml

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

https://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/context

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

添加Dog類

public class Dog {

private String name;

private String sex;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getSex() {

return sex;

}

public void setSex(String sex) {

this.sex = sex;

}

@Override

public String toString() {

return "entity.Dog [name=" + name + ", sex=" + sex + "]";

}

}

添加工廠類

@Configuration

@ComponentScan(basePackages = "entity")

public class BeansConfig {

@Bean

public Dog getDog(){

Dog dog = new Dog();

dog.setSex("男");

dog.setName("王二麻子");

return dog;

}

}

添加測(cè)試類

public static void main(String[] args) {

AnnotationConfigApplicationContext configApplicationContext =

new AnnotationConfigApplicationContext(BeansConfig.class);

Object dog = configApplicationContext.getBean("getDog");

System.out.println(dog);

}

總結(jié)

以上是生活随笔為你收集整理的java 日期注解 xml_Spring xml注解+java注解的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。