` 错误消息我收到错误:-NoClassDefFoundError
12:19:07.250 [主要]调试 org.springframework.context.support.ClassPathXmlApplicationContext - 刷新 org.springframework.context.support.ClassPathXmlApplicationContext@24b1d79b 线程“main”中的异常 java.lang.NoClassDefFoundError: org/springframework/core/metrics/ApplicationStartup 在org.springframework.beans.factory.support.AbstractBeanFactory。(AbstractBeanFactory.java:184) 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory。(AbstractAutowireCapableBeanFactory.java:176) 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory。(AbstractAutowireCapableBeanFactory.java:193) 在 org.springframework.beans.factory.support.DefaultListableBeanFactory。(DefaultListableBeanFactory.java:201) 在org.springframework.context.support.AbstractRefreshableApplicationContext.createBeanFactory(AbstractRefreshableApplicationContext.java:197) 在org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:127) 在org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:638) 在org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:523) 在org.springframework.context.support.ClassPathXmlApplicationContext。(ClassPathXmlApplicationContext.java:144) 在 org.springframework.context.support.ClassPathXmlApplicationContext。(ClassPathXmlApplicationContext.java:85) 在 jdbc.mainioc.main(mainioc.java:12) 引起原因:java.lang.ClassNotFoundException:org.springframework.core.metrics.ApplicationStartup 在 java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641) 在 java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188) 在 java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:525) ... 11 更多
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
public class mainioc
{
public static void main(String[] args)
{
ApplicationContext ctx=new ClassPathXmlApplicationContext("Appmeta.xml");
JdbcTemplate temp= (JdbcTemplate) ctx.getBean("dataS");
//insert query
String query="insert into employee(id,name,salary) values(?,?,?)";
//fire query
int result=temp.update(query,3,"ram",14578);
System.out.println("record inserted"+ result );
}
}
i try to add dependency spring starter
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.3.3.RELEASE</version>
</dependency>
myentity class
package com.spjdbc.entity;
public class student
{
private String id;
private String Sname;
private String Srollno;
@Override
public String toString()
{
return "student [id=" + id + ", Sname=" + Sname + ", Srollno=" + Srollno + "]";
}
public String getId()
{
return id;
}
public void setId(String id)
{
this.id = id;
}
public String getSname()
{
return Sname;
}
public void setSname(String sname)
{
Sname = sname;
}
public String getSrollno()
{
return Srollno;
}
public void setSrollno(String srollno)
{
Srollno = srollno;
}
}
meta file
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="DataS" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/studentdb"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<bean id="jdbcB" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="DataS"/>
</bean>
</beans>
test file
package com.spjdbc.ioc;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
public class iocmain {
public static void main(String[] args)
{
ApplicationContext cont= new AnnotationConfigApplicationContext("com/spjdbc/ioc/BeanConf.class");
JdbcTemplate temp= cont.getBean(JdbcTemplate.class);
String query="insert into student(id,Sname,Srollno) values(?,?,?)";
int result = temp.update(query,1,"ram",24001);
System.out.println("intert the value" +result);
}
}
Annotation
package com.spjdbc.ioc;
import javax.sql.DataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
@Configuration
public class BeanConf
{
@Bean
public DataSource dataSource()
{
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/studentdb");
dataSource.setUsername("root");
dataSource.setPassword("root");
return dataSource;
}
@Bean
public JdbcTemplate jdbcTemplate()
{
return new JdbcTemplate(dataSource());
}
}
how can i resolve the issue Bean not accessesible itry both the form XML aand Annotation but cant work
您需要将 Spring Data JPA 的依赖项以及要使用的数据库连接器 (MySQL) 添加到您的
pom.xml
。:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>