Spring(2):Spring 은 IOC 를 통 해 대상 을 만 듭 니 다.
1.1 Spring 은 어떻게 대상 을 얻 습 니까?
① Maven 프로젝트 를 새로 만 든 후 웹 뮤 직 비디오 c 를 가 져 오 는 의존:웹 뮤 직 비디오 c 는 다른 의존 도 를 많이 포함 하고 있 기 때문에 편리 하 게 가 져 옵 니 다!버 전 이 잖 아!개인 적 으로 최신 판 을 즐겨 씁 니 다.
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.5</version>
</dependency>
② 새 실체 테스트 클래스:
public class Person {
private String name;
private int age;
private String like;
private String high;
//get、set、tostring , lombok
}
③ resources 디 렉 터 리 에 ContextAplication.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="Person" class="entity.Person">
<property name="age" value="23"></property>
<property name="name" value=" "></property>
<property name="like" value=" "></property>
<property name="high" value="173"></property>
</bean>
</beans>
④ 이상 의 전 제 를 거 친 후에 테스트 Person 종류 에 변화 가 생 겼 음 을 발견 할 수 있 습 니 다.클릭 하면 지정 한 xml 위치 로 이동 할 수 있 습 니 다~⑤ 테스트:
Context.getBean()이 클래스 를 지정 하지 않 을 때 강제 변환 이 필요 하기 때문에 두 번 째 방식 으로 대상 을 가 져 오 는 것 을 권장 합 니 다.
public class Test {
public static void main(String[] args) {
ApplicationContext Context = new ClassPathXmlApplicationContext("ContextAplication.xml");
// Person person = (Person) Context.getBean("Person");// ,
Person person = Context.getBean("Person",Person.class);
System.out.println(person);
}
}
⑥ 실행 결 과 는 다음 과 같 습 니 다:성공 적 으로 값 을 받 았 습 니 다!⑦ 총괄:
①xml:
<bean id="StudentMapperImpl" class="mapper.impl.StudentMapperImpl"/>
<bean id="TeacherMapperImpl" class="mapper.impl.TeacherMapperImpl"/>
<bean id="PersonServiceImpl" class="service.impl.PersonServiceImpl">
<property name="studentMapper" ref="StudentMapperImpl"/>
</bean>
② 테스트:
ApplicationContext Context1 = new ClassPathXmlApplicationContext("ContextAplication.xml");
PersonServiceImpl personServiceImpl = Context1.getBean("PersonServiceImpl", PersonServiceImpl.class);
personServiceImpl.getPersonInfo();
③ 실행 결과:⑤ 요약:
대상 은 Spring 에서 만 들 고 관리 하 며 조립 합 니 다!이게 IOC 야!
2.IOC 는 어떤 방식 으로 대상 을 만 듭 니까?
2.1 무 참 구조 함수 로 대상 만 들 기
① Person 류 를 예 로 들 지만 무 참 구조 함 수 를 추가 합 니 다!
public class Person {
private String name;
private int age;
private String like;
private String high;
public Person() {
// !
System.out.println(" Person ! !!!!");
}
//set、get、tostring , !
}
② 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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="Person" class="entity.Person">
<property name="age" value="23"></property>
<property name="name" value=" "></property>
<property name="like" value=" "></property>
<property name="high" value="173"></property>
</bean>
</beans>
③ 테스트 클래스:
public class Test {
public static void main(String[] args) {
ApplicationContext Context = new ClassPathXmlApplicationContext("ContextAplication.xml");
Person person = Context.getBean("Person", Person.class);
System.out.println(person);
}
}
④ 집행 결과:⑤ 무 삼 구 조 를 제거 하고 유 삼 구 조 를 늘린다.
xml 설정 프로그램 오류 직접 보고:
⑥ 결론:
Spring 생 성 대상 은 기본적으로 무 참 구조 함수 로 만 들 어 졌 습 니 다!구조 함수 가 있어 서 대상 을 만 들 수 있 습 니까?네!아래 봐!
2.2 참조 구조 방법 으로 대상 만 들 기
① 전 제 는 2.1 이 일치 하 는 것 을 전제 로 삼 구조 함수 가 새로 추 가 됩 니 다.(클래스 에서 기본 적 인 것 은 구조 적 인 파 라 메 터 를 쓰 지 않 으 면 무 삼 구조 이 고 삼 구조 가 있어 야 진정한 의미 에서 무 삼 구 조 를 제거 할 수 있 기 때 문 입 니 다.이 건 너무 많이 설명 하지 않 아 도 됩 니 다.자바 기반 의 내용 입 니 다~!)
public Person(String name, int age, String like, String high) {
this.name = name;
this.age = age;
this.like = like;
this.high = high;
}
② 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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="Person" class="entity.Person">
<!-- <property name="name" value=" "></property>-->
<!-- <property name="age" value="23"></property>-->
<!-- <property name="like" value=" "></property>-->
<!-- <property name="high" value="173"></property>-->
<constructor-arg index="0" value=" "/>
<constructor-arg name="age" value="23"/>
<constructor-arg type="java.lang.String" value=" "/>
<constructor-arg type="java.lang.String" value="173"/>
</bean>
</beans>
③ 실행 결과:⑤ 요약:
3.1 alias(별명):
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<alias name="Person" alias="personAlias1"/>
<alias name="Person" alias="personAlias2"/>
<alias name="Person" alias="personAlias3"/>
<bean id="Person" class="entity.Person">
<constructor-arg index="0" value=" "/>
<constructor-arg name="age" value="23"/>
<constructor-arg type="java.lang.String" value=" "/>
<constructor-arg type="java.lang.String" value="173"/>
</bean>
</beans>
② 테스트 클래스:
public class Test {
public static void main(String[] args) {
ApplicationContext Context = new ClassPathXmlApplicationContext("ContextAplication.xml");
Person person = Context.getBean("personAlias1", Person.class);
System.out.println(person);
}
}
③ 실행 결과:④ 결론:솔직히 이 물건 은 쓸모 가 많 지 않 습 니 다.별명 을 설정 하 는 더 좋 은 방법 이 있 기 때 문 입 니 다!
3.2 Bean 의 설정:
①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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<alias name="Person" alias="personAlias1"/>
<alias name="Person" alias="personAlias2"/>
<alias name="Person" alias="personAlias3"/>
<bean id="Person" name="person1,person2 person3;person4" class="entity.Person">
<constructor-arg index="0" value=" "/>
<constructor-arg name="age" value="23"/>
<constructor-arg type="java.lang.String" value=" "/>
<constructor-arg type="java.lang.String" value="173"/>
</bean>
</beans>
② 테스트 클래스:
public class Test {
public static void main(String[] args) {
ApplicationContext Context = new ClassPathXmlApplicationContext("ContextAplication.xml");
Person person = Context.getBean("person4", Person.class);
System.out.println(person);
}
}
③ 실행 결과:④ 정리:
3.3 import(팀워크 도입)
① 실제 작업 의 개발 과정 에서 하나의 프로젝트 는 여러 프로그래머 가 개발 할 수 있 기 때문에 공통성 문 제 를 해결 하기 위해 서 입 니 다.예 를 들 어 같은 파일 을 제출 할 때 수정 을 하면 충돌 을 일 으 킬 수 있 기 때문에 우 리 는 import 를 사용 하여 결합 을 해제 합 니 다!
② 여러 xml 프로필 을 새로 만 듭 니 다.
ContextAplication.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<import resource="dyj1.xml"/>
<import resource="dyj3.xml"/>
<import resource="dyj2.xml"/>
</beans>
dyj1.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean name="person1,person2 person3;person4" class="entity.Person">
<constructor-arg index="0" value=" 1"/>
<constructor-arg name="age" value="23"/>
<constructor-arg type="java.lang.String" value=" 1"/>
<constructor-arg type="java.lang.String" value="173"/>
</bean>
</beans>
dyj2.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean name="person1,person2 person3;person4" class="entity.Person">
<constructor-arg index="0" value=" 2"/>
<constructor-arg name="age" value="23"/>
<constructor-arg type="java.lang.String" value=" 2"/>
<constructor-arg type="java.lang.String" value="173"/>
</bean>
</beans>
dyj3.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean name="person1,person2 person3;person4" class="entity.Person">
<constructor-arg index="0" value=" 3"/>
<constructor-arg name="age" value="23"/>
<constructor-arg type="java.lang.String" value=" 3"/>
<constructor-arg type="java.lang.String" value="173"/>
</bean>
</beans>
③ 실행:④ 정리:
이 글 은 여기까지 입 니 다.당신 에 게 도움 이 되 기 를 바 랍 니 다.또한 당신 이 우리 의 더 많은 내용 에 관심 을 가 져 주 기 를 바 랍 니 다!
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
[MeU] Hashtag 기능 개발➡️ 기존 Tag 테이블에 존재하지 않는 해시태그라면 Tag , tagPostMapping 테이블에 모두 추가 ➡️ 기존에 존재하는 해시태그라면, tagPostMapping 테이블에만 추가 이후에 개발할 태그 기반 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.